[Interest] How to insert a new record with generated primary key into a QSqlTableModel

Scott Aron Bloom scott.bloom at onshorecs.com
Thu Dec 12 17:42:12 CET 2013


SQLLite does.. after the insert command is executed, call lastInsertId()

Scott

-----Original Message-----
From: interest-bounces+scott.bloom=onshorecs.com at qt-project.org [mailto:interest-bounces+scott.bloom=onshorecs.com at qt-project.org] On Behalf Of Knut Krause
Sent: Thursday, December 12, 2013 10:32 PM
To: interest at qt-project.org
Subject: Re: [Interest] How to insert a new record with generated primary key into a QSqlTableModel

I'm using SQLite, hopefully it'll support it. Where would I get the
QSqlResult::lastInserId() though? I really don't want my database to be in some inconsistent state if I do something wrong or if I abort the edit the record should be discarded again.

regards

Am Donnerstag, 12. Dezember 2013, 14:25:24 schrieb Scott Aron Bloom:
> It will depend slightly on the database.
> 
> If your database driver back end supports the driver feature 
> LastInsertId, then the easiest way is to call 
> QSqlResult::lastInserId()
> 
> You can then do a fetchMore() on the model, and correlate the id to 
> the index...
> 
> if not, I have done a search for the recently inserted record, get the 
> id, and do the same correlation
> 
> Scott
> 
> -----Original Message-----
> From: interest-bounces+scott=onshorecs.com at qt-project.org
> [mailto:interest-bounces+scott=onshorecs.com at qt-project.org] On Behalf 
> Of Knut Krause Sent: Thursday, December 12, 2013 10:16 PM
> To: interest at qt-project.org
> Subject: [Interest] How to insert a new record with generated primary 
> key into a QSqlTableModel
> 
> Hi,
> 
> I got a DataMapper set up like http://pastebin.com/k9kB1N1a Editing 
> the existing model works really fine but how can I add a new customer? 
> I basically need to insert a new record to the model with an auto 
> generated id (from my DB) and then set the mapper to the correct 
> ModelIndex. How would you do that?
> 
> regards
> 
> 
> Knut
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
_______________________________________________
Interest mailing list
Interest at qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest



More information about the Interest mailing list