开发者

qt sqlite select statement

开发者 https://www.devze.com 2023-03-18 16:04 出处:网络
I decided to test sqlite db for my Qt application. I have created 开发者_高级运维the sqlite db file with the proper statements (create table etc. and Inserted some rows of data).

I decided to test sqlite db for my Qt application.

I have created 开发者_高级运维the sqlite db file with the proper statements (create table etc. and Inserted some rows of data). My problem is that when I execute a select statement I don't get any records.

This is the code I use:

qq.sprintf("SELECT * from descriptors WHERE descriptors.id=%d ",idx);
query.exec(qq);
if( query.isSelect() ){
    while (query.next()){

    int fff = query.value(0).toInt();

}}

The problem is that I never get inside the while loop. query.next() doesn't seem to work.

any hints? thanks in advance, Thodoris

p.s. I forgot to write my configuration so: Qt 4.7.3, windows 7, visual studio 2008


Other than the mistake hexa posted, query.isSelect() will always return true even if the query failed. You need to check the result of exec():

QSqlQuery query;
query.prepare( "SELECT * FROM descriptors WHERE id = ?" );
query.bindValue( 0, idx ); // assuming idx is an integer/long/QVariant value

if( !query.exec() )
{
  // Error Handling, check query.lastError(), probably return
}

// Note: if you don't return in case of an error, put this into the else{} part
while( query.next() )
{
    int fff = query.value( 0 ).toInt();
}


In my case, backward iteration over QSqlQueries worked. I think this could be a bug somewhere in the QSQLite Driver implementation.

QSqlQuery q = db.exec("SELECT * FROM Table");
if (q.last()) {
    do {
        // Do something with row...
    } while (q.previous());
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号