[Qt-interest] QSqlTableModel and autoincrement fields
Scott Aron Bloom
Scott.Bloom at onshorecs.com
Mon Oct 25 08:45:24 CEST 2010
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20101024/8ff77f19/attachment.html
More information about the Qt-interest-old
mailing list