On Fri, 9 Nov 2001, Carl Troein wrote:

> Date: Fri, 09 Nov 2001 15:41:21 GMT
> From: Carl Troein <[EMAIL PROTECTED]>
> To: [EMAIL PROTECTED]
> Subject: Re: Beginner question - getting last inserted ID
> 
> 
> Anna Åhnberg writes:
> 
> > Thanks, I actually already found the chapters but now I also now how to
> > use the function too!
> 
> Let me quote from the manual:
> "LAST_INSERT_ID([expr]) 
>      Returns the last automatically generated value that was
>      inserted into an AUTO_INCREMENT column.
> mysql> select LAST_INSERT_ID();
>              -> 195"
> 
> Thus you'd follow these steps:
> 1) INSERT into the table, and leave out the auto column or supply
>    a value of 0 or NULL
> 2) Verify that the query succeeded
> 3) SELECT LAST_INSERT_ID()
> 4) Get the result of the SELECT. On success, the SELECT will
>    return one row with one value in it. That is the number you're
>    interested in knowing.
If you are using the mmysql jdbc driver you can directly access it.

use 
import org.gjt.mm.mysql.Statement;
instead of "java.sql.Statement;"

Then after the insert

      long id = stmt.getLastInsertID();
Note: this is the only method to get a bigint unsigned index back 
accordign to the mm.mysql doc's .  See source forge for details.

> 
> If you were using MySQL's C API there is a function that returns
> the ID without the need for steps 3-4, but since you are
> communicating over JDBC I don't think there's a simpler solution
> available. The query in step 3 is extremely fast, so it doesn't
> really matter, except that it's a bit more work to make an extra
> query.
> 
> //C
> 
> -- 
>  Carl Troein - Círdan / Istari-PixelMagic - UIN 16353280
>  [EMAIL PROTECTED] | http://pixelmagic.dyndns.org/~cirdan/
>  Amiga user since '89, and damned proud of it too.
> 
> 
> ---------------------------------------------------------------------
> Before posting, please check:
>    http://www.mysql.com/manual.php   (the manual)
>    http://lists.mysql.com/           (the list archive)
> 
> To request this thread, e-mail <[EMAIL PROTECTED]>
> To unsubscribe, e-mail <[EMAIL PROTECTED]>
> Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php
> 

Sincerely,

William Mussatto, Senior Systems Engineer
CyberStrategies, Inc
ph. 909-920-9154 ext. 27


---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to