Almost each database has its own syntax to get last selected row id. So your question is not QSqlQuery's business but SQL statement. You could use SELECT @@IDENTITY for MSSQL, SELECT LAST_INSERT_ID() for MYSQL, SELECT seq.nextval for Oracle, etc.
SELECT MAX(id) may get incorrect id if it is used in mutil-threaded systems, IMO. Well, I'm not sure about this. Cheng Liang Nanjing, China http://www.devbean.net From: adrian.st...@screenfood.com To: wewol...@gmail.com; interest@qt-project.org Date: Wed, 22 May 2013 08:31:47 +0000 Subject: Re: [Interest] "Last selected row id" QSqlQuery? Take a look at this: http://blog.sqlauthority.com/2007/03/25/sql-server-identity-vs-scope_identity-vs-ident_current-retrieve-last-inserted-identity-of-record/ Freundliche Grüsse / Best Regards / Meilleures salutations Adrian Stern From: interest-bounces+adrian.stern=screenfood....@qt-project.org [mailto:interest-bounces+adrian.stern=screenfood....@qt-project.org] On Behalf Of Adrian Stern Sent: 22 May 2013 10:30 To: Witold E Wolski; interest@qt-project.org Subject: Re: [Interest] "Last selected row id" QSqlQuery? In MSSQL there is a command called SCOPE_IDENTITY() which returns the id of the last inserted row. There is probably something similar in whatever dbms you are using. Otherwise I would need some more information about our environment. Freundliche Grüsse / Best Regards / Meilleures salutations Adrian Stern From: interest-bounces+adrian.stern=screenfood....@qt-project.org [mailto:interest-bounces+adrian.stern=screenfood....@qt-project.org] On Behalf Of Witold E Wolski Sent: 22 May 2013 10:26 To: interest@qt-project.org Subject: [Interest] "Last selected row id" QSqlQuery? How can I get efficiently the id of the last insterted row? A the moment I am using : SELECT MAX(id) from table. Which isn't efficient. How can i do it more efficiently but stay database agnostic. best Witold Diese E-Mail und ihre Anhänge enthalten vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail inklusive Anhänge. Das unerlaubte Kopieren sowie die unbefugte Weitergabe der Inhalte dieser Mail ist nicht gestattet. This e-mail and any attachments may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail including the attachments. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden. Diese E-Mail und ihre Anhänge enthalten vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail inklusive Anhänge. Das unerlaubte Kopieren sowie die unbefugte Weitergabe der Inhalte dieser Mail ist nicht gestattet. This e-mail and any attachments may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail including the attachments. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden. _______________________________________________ Interest mailing list Interest@qt-project.org http://lists.qt-project.org/mailman/listinfo/interest
_______________________________________________ Interest mailing list Interest@qt-project.org http://lists.qt-project.org/mailman/listinfo/interest