[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 560
  • Last Modified:

MySQL - AUTO INCREMENT

After I insert a row by SQL statement, a auto increment value will be assigned. How can I retrive this auto increment value ?

Andrew
0
andrewyu
Asked:
andrewyu
1 Solution
 
intheCommented:
hi,
 you may be able to use mysql_insert_id :
http://www.mysql.com/doc/m/y/mysql_insert_id.html

also maybe of interest:
http://www.mysql.com/doc/e/x/example-AUTO_INCREMENT.html

else i guess a stored procedure ,i dunno about using them but someone (like meikl :) be along soon enough..
0
 
intheCommented:
sorry i was thinking php ,i meant use a select statement with LAST_INSERT_ID()instead.

http://www.mysql.com/doc/O/D/ODBC_and_last_insert_id.html
http://www.mysql.com/doc/G/e/Getting_unique_ID.html
0
 
andrewyuAuthor Commented:
I tried LAST_INSERT_ID, but, raise a DBException.

Andrew
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
intheCommented:
how are you connecting to mysql
0
 
intheCommented:
hi,
i tested an example ,it works fine here :

procedure TForm1.Table1AfterPost(DataSet: TDataSet);
var
 i:  integer;
begin
 Query1.sql.add('insert into testtable(name)');
 Query1.sql.add(' values(''barry'')');
 Query1.execSql;
 Query1.close;
 Query1.sql.clear;
 Query1.sql.add('SELECT LAST_INSERT_ID()');
 Query1.open;
 Query1.first;
 i := Query1.fields[0].asInteger;
 form1.caption := inttostr(i);
end;
0
 
intheCommented:
ps,
you can delete the first three lines
(i tested on a button first :)
0
 
andrewyuAuthor Commented:
I had already tried it and it raised a exception.

I use dbExpress to establish connection with mysqld-max-nt.

Andrew
PS:they are running on same Windows XP Professional machine
0
 
intheCommented:
hi andrew,
sorry i dont have dbExpress to try it out.
someone else may have idea ,i guess not many people here are using mysql,normally db questions have lots of commenters..
0
 
pnh73Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

PAQ/Refund

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Paul (pnh73)
EE Cleanup Volunteer
0
 
YensidModCommented:
Question is PAQ'd and points refunded.

YensidMod
Community Support Moderator @Experts Exchange
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now