[Qt-interest] QSqlRecord pqsql & INSERT
Jan
janusius at gmx.net
Thu Dec 24 17:18:35 CET 2009
it's ...
SELECT pg_catalog.setval('tbl_id_seq', 3, true)
what helps.
(know I know even more why I like sqlite so much)
Jan schrieb:
> Bill King schrieb:
>> yes, insert the record, but don't set anything in that field.
>> QSqlQuery::lastInsertId () will return you the value of the field from
>> the last insert so that you can then use this if necessary.
>
> The problem seems to be that autoincrement (r.setGenerated(0, false))
> does not work if the table creation is within the same db connection +
> inserting values in the serial column. No idea if this is a postgres
> (8.4) problem (I have not much experience with postgres) or a qt (4.6)
> driver problem (or my own problem):
>
>
> QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
> db.setHostName("localhost");
> db.setUserName("postgres");
> db.setPassword("postgres");
> db.setDatabaseName("postgres");
> if (db.open())
> {
> db.transaction();
> QSqlQuery q;
> q.exec("CREATE TEMP TABLE tbl (id SERIAL PRIMARY KEY, name TEXT
> UNIQUE NOT NULL)");
>
> //submitAll() fails due to serial unique constraint
> q.exec("INSERT INTO tbl VALUES(1, 'name1')");
> q.exec("INSERT INTO tbl VALUES(2, 'name2')");
> q.exec("INSERT INTO tbl VALUES(3, 'name3')");
>
> //submitAll() fails not
> // q.exec("INSERT INTO tbl (name) VALUES('name1')");
> // q.exec("INSERT INTO tbl (name) VALUES('name2')");
> // q.exec("INSERT INTO tbl (name) VALUES('name3')");
>
> db.commit();
>
> QSqlTableModel *model = new QSqlTableModel;
> model->setTable("tbl");
> model->select();
> model->setEditStrategy(QSqlTableModel::OnManualSubmit);
>
> QSqlRecord r = model->record();
> r.setGenerated(0, false);
> r.setValue(1, "name4");
> model->insertRecord(-1, r);
> model->submitAll();
> qDebug() << model->lastError().text();
>
> delete model;
> }
>
>
> merry christmas btw
> Jan
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
More information about the Qt-interest-old
mailing list