PDA

View Full Version : PostgreSql and problem with lastInsertId()



Hostel
9th September 2011, 02:28
I have a problem with method lastInsertId. I'm using a PostgreSql 8.4 and Qt 4.6.3 under Linux. I have a code like this:


// _model is a QSqlTableModel
QSqlRecord record = _model->record();
_model->insertRecord( -1, record );

SomeDialog* dialog = new SomeDialog( _model );
dialog->exec();
if( dialog->result() == QDialog::Accepted )
{
bool ok = _model->submitAll();
if( ok )
{
// allways 0 but in db is a new record
qDebug() << _model->query().lastInsertId().toInt();
}
else
{
qDebug() << _model->lastError().text();
}
}


My table looks like this:


CREATE TABLE mytable
(
id serial NOT NULL,
field1 text,
CONSTRAINT mytable_pkey PRIMARY KEY (id),
)
WITH (
OIDS=TRUE
);


How can I get a inserted ID? I need select new row in QTableView and I need this information.