sql - LastInstertId

BlitzMax Forums/Brucey's Modules/sql - LastInstertId

Retimer(Posted 2008) [#1]
Not entirely sure how to achieve this. I'm trying to get the primary id of the row I just inserted into an sql database.

Local query:TDatabaseQuery = MyDB.executeQuery("INSERT INTO npcs (npcname) values ('new')") 
notify query.lastInsertedId() 



notify query.lastInsertedId() gives the error. I've tried a few things but with no luck as to getting the last inserted id. Any clues?

Thanks


Brucey(Posted 2008) [#2]
What database type, and what's your table schema look like?


Retimer(Posted 2008) [#3]
MySQL

The query runs successfully, i'm just unsure how to get that lastinsertid.

The table:



Wayne(Posted 2008) [#4]
Does this help?
http://dev.mysql.com/doc/refman/4.1/en/getting-unique-id.html


Retimer(Posted 2008) [#5]
I'm hoping I can retrieve it without an additional query. I'm pretty sure additional info is passed when you execute an insert, that returns the last insert id. I'm just not sure how to get it through bruceys mod.

I'm also not using the id for in a query (since the database handles auto increments), but wanting to store it in a variable. Thanks for checking that out though


Brucey(Posted 2008) [#6]
Hi,

There's a fixed version of the module available via SVN at http://code.google.com/p/maxmods/

Am in the progress of migrating the modules to the googlecode site, so I'll hopefully have it available as a "normal" download from there shortly.


Retimer(Posted 2008) [#7]
You are by far the best developer & supporter i've ever seen to date, lol. Downloading now..

Edit:
Without surprise, it works perfect. Can't thank you enough!

Time


nadia(Posted 2008) [#8]
I fully agree with the last statement by Retimer!
Brucey's knowledge, support and speed of fixing bugs is quite astonishing. Does that guy ever sleep or get some rest? It hardly ever takes more then a few hours and he got a bugfix ready.

I've also encountered the above mentioned problem. I just used
"SELECT MAX(id) FROM mytable"
to retrieve the new auto-incremented id after inserting a new record, which is of course a rather lame hack if the database is accessed by multiple users.
So I'm very happy hat LastInsertedID() got fixed, thanx Brucey!!


Brucey(Posted 2008) [#9]
Does that guy ever sleep or get some rest?

Not if my neighbours have anything to do with it.... used to be a nice, quiet corner of Pretoria too... ho hum.


Anyhoo... :-)

I've posted the latest releases proper now - http://code.google.com/p/maxmods/downloads/list
(if you have the latest SVN, they are the same).

The latest MySQL module for Win32 now doesn't require the static library to be copied into BlitzMax's lib folder. Which makes for less fiddling and nonsense.

SQLite module has had a lot of work recently, since I've been messing around with it a lot - funny how you pick up problems when you actually use these!

:o)

As always, please feel free to post any issues etc.