PDA

View Full Version : QSqlQuery read row id (SOLVED)



josepvr
18th May 2011, 14:24
hello,
i'm implementing a routine that reads row data from SQLITE db.
I want to read id and the other fields.
I can get other fields data correctly, but id data is null.

my code is:


QSqlQuery query("SELECT id,name FROM person");

while (query.next())
{
qDebug(query.value(0).toString().toAscii());
qDebug(query.value(1).toString().toAscii());
}


and result is:


<-- nothing
Tom <-- first row name column value.


Can anybody help me?

bibhukalyana
18th May 2011, 14:47
try


int id;
id=query.record().value("id").toInt();


use #include <QSqlRecord>

josepvr
18th May 2011, 14:56
I've solved the problem!

problem was creating table, auto increment wasn't set to id field.


query.exec("create table person(id INTEGER PRIMARY KEY AUTOINCREMENT,namevarchar(20))");


Thanks!

bibhukalyana
18th May 2011, 14:59
Without auto increment you can create table .