Solved

MySQL - AUTO INCREMENT

Posted on 2002-05-01
10
549 Views
Last Modified: 2010-04-04
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
Comment
Question by:andrewyu
10 Comments
 
LVL 17

Expert Comment

by:inthe
ID: 6985285
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
 
LVL 17

Expert Comment

by:inthe
ID: 6985302
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
 

Author Comment

by:andrewyu
ID: 6985308
I tried LAST_INSERT_ID, but, raise a DBException.

Andrew
0
 
LVL 17

Expert Comment

by:inthe
ID: 6985317
how are you connecting to mysql
0
 
LVL 17

Expert Comment

by:inthe
ID: 6985421
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 17

Expert Comment

by:inthe
ID: 6985425
ps,
you can delete the first three lines
(i tested on a button first :)
0
 

Author Comment

by:andrewyu
ID: 6985968
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
 
LVL 17

Expert Comment

by:inthe
ID: 6988070
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
 
LVL 1

Expert Comment

by:pnh73
ID: 9004484
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
 

Accepted Solution

by:
YensidMod earned 0 total points
ID: 9096610
Question is PAQ'd and points refunded.

YensidMod
Community Support Moderator @Experts Exchange
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now