Hi,
How do I do this? I'm using MySQL and I'm trying something similar to the following:
INSERT INTO worlditems(...) VALUES(...); SELECT LAST_INSERT_ID()
but while it works when run directly against the database, it fails in smartfox's DbManager.
I need the LAST_INSERT_ID because an entry in another table needs to refer to it.
How to get last inserted ID from auto_increment table?
No, if another connection/thread adds a row between your select query and your insert you will get the wrong ID. It also adds an extra SELECT which is unnecessary.Flappi282 wrote:Try this
SELECT * FROM tablename ORDER BY primarykeyidfield DESC LIMIT 1;
That will return the last row inserted. You can get the ID etc from that
This actually should work, except that the LAST_INSERT_ID() is per Connection, and in SmartFox the database Connection is actually shared among separate threads; is this correct? I would need additional synchronization locks every time there's an insert, with a possibility for error should anyone forget to add the lock.BigFIsh wrote: SELECT LAST_INSERT_ID() AS LASTID;
Maybe I'll just wrap it in an accessor that locks or something.