[Qt-interest] QSqlRecord pqsql & INSERT

Jan janusius at gmx.net
Thu Dec 24 21:01:56 CET 2009


You are right: setGenerated() is not necessary.

The problem was that postgres does not increment the sequence 
(nextval()) if I manually insert id values (I need to manually populate 
other tables with foreign keys).

If I execute  q.exec("SELECT pg_catalog.setval('tbl_id_seq', 3, true)");
I can insert the record like this:

QSqlRecord r = model->record();
r.setValue(1, "name4");
model->insertRecord(-1, r);
model->submitAll();


Jan

Israel Brewster schrieb:
> 
> 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.
>>
>>    QSqlRecord r = model->record();
>>    r.setGenerated(0, false);
>>    r.setValue(1, "name4");
>>    model->insertRecord(-1, r);
>>    model->submitAll();
> 
> If you just want to insert a record for "name4", all you need to do is 
> the following:
> 
> QSqlRecord r = model->record();
> r.setValue(1, "name4");
> model->insertRecord(-1, r);
> model->submitAll();
> 
> Since you didn't set any value for the first field, PostgreSQL (or any 
> of the other databases for that matter) will automatically populate it 
> (since it is an auto-increment). I'm actually not even sure what the 
> setGenerated function does or when you would want to call it - I have 
> made a number of PostgreSQL Qt applications (and driven Bill King nuts 
> when I run into issues ;-) ), and never needed to use that function. All 
> my tables have an auto-increment column.
> 
>>    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
> -----------------------------------------------
> 
> 
> ------------------------------------------------------------------------
> 
> 
> 
> 
> 



More information about the Qt-interest-old mailing list