[Qt-interest] QSqlRecord pqsql & INSERT

Israel Brewster israel at frontierflying.com
Thu Dec 24 18:00:38 CET 2009


On Dec 24, 2009, at 3:59 AM, Jan wrote:

> 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')");

Why are you trying to manually insert serial values? You're just  
asking for problems exactly like what you are getting if you do. Let  
PoastgresQL handle the population of the column, and just do exactly  
what you have below:

>
>     //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')");
>

As Bill King mentioned in his response, if you need to know what ID  
the record was given, you can use QSqlQuery::lastInsertId ().

>    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

-----------------------------------------------
Israel Brewster
Computer Support Technician II
Frontier Flying Service Inc.
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7250 x293
-----------------------------------------------

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Israel Brewster.vcf
Type: text/directory
Size: 417 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091224/32671de7/attachment.bin 
-------------- next part --------------




More information about the Qt-interest-old mailing list