[Qt-interest] QSqlTableModel and autoincrement fields

Harry Sfougaris hsfougaris at gmail.com
Tue Oct 26 09:08:57 CEST 2010


With a QSqlQuery it works
    	QSqlQuery q;
    	q.prepare("INSERT INTO helpers.currencies (descr) VALUES ('test')");
    	q.exec();
    	qDebug() << "LID = " << q.lastInsertId();
outputs something like
	LID = QVariant(uint, 146865)
So do you think there's anything I can do to use it with QSqlTableModel?

Thanks,
Harry

On 25 Οκτ 2010, at 7:02 μ.μ., Scott Aron Bloom wrote:

> What is probably going on, is the insert is occurring then a select request, which is clearing the lasted result…
>  
> Try a simple call with QSqlQuery and see if it works…
>  
> From: Harry Sfougaris [mailto:hsfougaris at gmail.com] 
> Sent: Monday, October 25, 2010 12:42 AM
> To: Scott Aron Bloom
> Cc: qt-interest at trolltech.com
> Subject: Re: [Qt-interest] QSqlTableModel and autoincrement fields
>  
> Well like I said in my original post, what baffles me is that
> database().driver()->hasFeature(QSqlDriver::LastInsertId)
> returns true....
>  
> Are you using it also with a QSqlTableModel? Because I'm not even sure I'm calling it properly (through query()), but I haven't found any other way.
>  
> Thanks,
> Harry
>  
>  
> On 25 Οκτ 2010, at 9:45 π.μ., Scott Aron Bloom wrote:
> 
> 
> lastInsertId only works if the driver is setup properly..
>  
> Of course, not all SQL support it, check to see if the driver supports QSqlDriver::LastInsertId
>  
> Most I have played with don’t…
>  
> However, I have tweaked (for experimentation) and implemented the lastInsertId for MSSql… worked pretty well.. But you have to know exactly what yoru doing based on the server in question..
>  
> For instance, MSSql has a couple of ways to get it.. as does apparently postgres..
>  
> Scott
>  
> From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Harry Sfougaris
> Sent: Sunday, October 24, 2010 11:38 PM
> To: Nikos Gerontidis
> Cc: qt-interest at trolltech.com
> Subject: Re: [Qt-interest] QSqlTableModel and autoincrement fields
>  
> To bypass the issue I am using 
> SELECT currval('mySeqName')")
> after submitting, but since it introduces very postgresql specific code in my app I don't like it, and also it still leaves me wondering as to what is the purpose of lastInsertId() and what is required to make it work properly (or is it just a bug that I need to report).
>  
> Harry
>  
> On 25 Οκτ 2010, at 9:24 π.μ., Nikos Gerontidis wrote:
> 
> 
> 
> 
> Hello,
> 
> I am not sure of that, but I guess an alternative could be to get by somehow a new record's index and get the sibling's index that correspond to your auto increment field. Could look like this:
> 
> // after submitting
> ..
> QModelIndex newIndex = dataRecord ->rowCount();
> qint32 autoIncrementValue = newIndex.sibling(newIndex.row(),0).data().toInt();
> 
> 
> 
> 
> On Sun, Oct 24, 2010 at 10:43 PM, Harry Sfougaris <hsfougaris at gmail.com> wrote:
> Hi,
> How am I supposed to retrieve the value of an autoincrement field of the last inserted record in QSqlTableModel?
> I am trying to use it with a postgresql table, but I can't really make sense of it...
> I've created a table using OIDS=true as stated in the documentation:
>  
> CREATE TABLE helpers.currencies
> (
>   id serial NOT NULL,
>   descr character varying(100) NOT NULL,
>   CONSTRAINT currencies_pkey PRIMARY KEY (id),
>   CONSTRAINT currency_uq_descr UNIQUE (descr)
> )
> WITH (
>   OIDS=TRUE
> );
>  
>  
> In my code, I use
>     QSqlTableModel *dataRecord = new QSqlTableModel();
>  
>     dataRecord->setTable("helpers.currencies");
>  
>     dataRecord->insertRow(0);
>     QDataWidgetMapper *dwMapper = new QDataWidgetMapper;
>  
>     dwMapper->setModel(dataRecord);
>  
>     dwMapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
>  
>     dwMapper->addMapping(ui->curr_descr, dataRecord->fieldIndex("descr"));
>  
>     dwMapper.setCurrentIndex(0);
>     ui.curr_descr.setText("Test");
>     if (!dwMapper->submit()) {
>         QMessageBox::critical(0, QObject::tr("Error saving record"), dataRecord->lastError().text());
>         return false;
>     } else {
> qDebug() << "Last ID = " << dataRecord->query().lastInsertId()
> }
>  
> This successfully saves the record in the table/database, but the call to lastInsertId() returns an empty QVariant.
> Calling dataRecord->database().driver()->hasFeature(QSqlDriver::LastInsertId) returns true, so I don't know what else to check.
> I tried setting 
> - setGenerated to true and false for the field "id" in the beforeInsert signal, which doesn't seem to affect anything
> - setAutoValue to true for the field "id" in the beforeInsert signal, but it doesn't seem to work (calling isAutoValue returns false).
>  
> Thanks,
> Harry
>  
> 
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
> 
> 
> 
> 
> -- 
> Best Regards
> Nikos Gerontidis
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>  
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>  
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20101026/2892f846/attachment.html 


More information about the Qt-interest-old mailing list