Results 1 to 2 of 2

Thread: QSqlQuery and seek() - doesn't work on first call.

  1. #1
    Join Date
    Sep 2008
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QSqlQuery and seek() - doesn't work on first call.

    I am trying to use the QSqlQuery.seek() function. I am running into a problem in which the first time that I call seek after a query it doesn't seem to do anything. If I call it a second time it does what I would expect. If I call next() before seek() it also behaves as expected. What is going on here?

    I am using the SQLite database for this and programing in PyQt4.

    Example Code:
    Qt Code:
    1. print 'is_active', self.query.isActive()
    2. print 'is_select', self.query.isSelect()
    3. print 'is_valid', self.query.isValid()
    4.  
    5. print ''
    6. print 'seek:', self.query.seek(0)
    7. print 'is_valid', self.query.isValid()
    8.  
    9. print ''
    10. print 'seek:', self.query.seek(0)
    11. print 'is_valid', self.query.isValid()
    12.  
    13. print ''
    14. print 'next', self.query.next()
    15. print 'is_valid', self.query.isValid()
    16.  
    17. print ''
    18. print 'seek:', self.query.seek(0)
    19. print 'is_valid', self.query.isValid()
    To copy to clipboard, switch view to plain text mode 

    Result:
    Qt Code:
    1. is_active True
    2. is_select True
    3. is_valid False
    4.  
    5. seek: False
    6. is_valid False
    7.  
    8. seek: True
    9. is_valid True
    10.  
    11. next False
    12. is_valid False
    13.  
    14. seek: True
    15. is_valid True
    To copy to clipboard, switch view to plain text mode 

    Thanks for any help with this,

    amicitas

  2. #2
    Join Date
    Sep 2008
    Posts
    60
    Thanks
    8
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery and seek() - doesn't work on first call.

    I tested your code on PostgreSQL using C++ and got correct results (I used a query that returns a single record, looks like you did the same). May be there is a bug in SqlLite driver or in PyQt4?

    Test program
    Qt Code:
    1. int main(int argc, char* argv[])
    2. {
    3. QCoreApplication app(argc, argv);
    4.  
    5. QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
    6. db.setDatabaseName("pm");
    7.  
    8. bool ok = db.open();
    9. if(! ok) {
    10. qDebug() << db.lastError().text();
    11. return 1;
    12. }
    13.  
    14. QSqlQuery query;
    15. if(query.exec("select * from pm.portfolio where id = 1")) {
    16. qDebug() << "is_active" << query.isActive();
    17. qDebug() << "is_select" << query.isSelect();
    18. qDebug() << "is_valid" << query.isValid();
    19.  
    20. qDebug() << "";
    21. qDebug() << "seek:" << query.seek(0);
    22. qDebug() << "is_valid" << query.isValid();
    23.  
    24. qDebug() << "";
    25. qDebug() << "seek:" << query.seek(0);
    26. qDebug() << "is_valid" << query.isValid();
    27.  
    28. qDebug() << "";
    29. qDebug() << "next" << query.next();
    30. qDebug() << "is_valid" << query.isValid();
    31.  
    32. qDebug() << "";
    33. qDebug() << "seek:" << query.seek(0);
    34. qDebug() << "is_valid" << query.isValid();
    35. }
    36. else {
    37. qDebug() << query.lastError();
    38. }
    39. }
    To copy to clipboard, switch view to plain text mode 

    Result
    is_active true
    is_select true
    is_valid false

    seek: true
    is_valid true

    seek: true
    is_valid true

    next false
    is_valid false

    seek: true
    is_valid true

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.