[Gambas-user] mysql last_insert_id

Caveat Gambas at ...1950...
Tue Jul 17 13:33:33 CEST 2012


Have you got auto_increment on the id field?

http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

Seems last_insert_id() looks at the previously-run SQL statement that 
performed an INSERT.  If you're running this query in a separate 
program, or have done other SQL operations since the INSERT on the 
editprojects table, you may well get 0 back.

Why not try select max(id) from editprojects?

Guess it depends what you really want... the id of the last-inserted 
record, or the maximum id used until now so you can assign the 'next' 
id... of course if you use auto_increment, you may not need the last id 
as it'll all happen automatically...

Kind regards,
Caveat

On 17/07/12 13:14, Bill-Lancaster wrote:
> I have a table (editprojects) with "id" as primary key, serial, integer.
> There is only one record at the moment (id = 1)
>       hResult = hConn.Exec("SELECT last_insert_id() as id FROM editprojects")
> print hResult!id
> produces 0 not 1
>
> Obviously my sql query is at fault but where?
>
> I wonder if anyone can help.






More information about the User mailing list