Results 1 to 3 of 3

Thread: Invalid QSqlRecord after performing another query to DB

  1. #1
    Join Date
    Apr 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Invalid QSqlRecord after performing another query to DB

    Hi everyone!

    I have some problems with using QtSql.
    So here's the code first:
    Qt Code:
    1. QSqlQuery q1(db_connection), q2(db_connection);
    2. q1.exec("SELECT * from users");
    3. while(q1.next())
    4. {
    5. q2.exec("delete from table1");
    6. if (!q2.isValid())
    7. qDebug() << "q2 is not valid";
    8.  
    9. if (q1.isActive() && q1.isValid())
    10. qDebug() << q1.record();
    11. }
    To copy to clipboard, switch view to plain text mode 

    So there are two queries to one database: q1 is absolutely correct and should return several result rows, but q1 is some wrong query.
    The question is why QSqlQuery::record() of q1 returns empty data after executing query q2?

    I'm using MySQL 5.5.
    I repeated this behaviour on qt 4.7.1 and qt 4.8.1 in Windows and Linux.

    P.S.: Sorry for my english.

  2. #2
    Join Date
    Apr 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Invalid QSqlRecord after performing another query to DB

    Any suggestions? :/

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Invalid QSqlRecord after performing another query to DB

    Works fine here with Sqlite but not MySql... it is going to be a MySQL quirk.

    Here is my take:
    Qt Code:
    1. #include <QtCore>
    2. #include <QtSql>
    3. #include <QDebug>
    4.  
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QCoreApplication app(argc, argv);
    9.  
    10. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    11. db.setDatabaseName("testdb");
    12. db.setUserName("user");
    13. db.setPassword("password");
    14. if (db.open()) {
    15. // Create a test table
    16. db.exec("CREATE TABLE zzz_test (a int, b varchar(10))");
    17. db.exec("INSERT INTO zzz_test VALUES (1, 'Apple')");
    18. db.exec("INSERT INTO zzz_test VALUES (2, 'Banana')");
    19. db.exec("INSERT INTO zzz_test VALUES (3, 'Cherry')");
    20.  
    21. QSqlQuery q1(db);
    22. QSqlQuery q2(db); // Fails badly if declared here
    23. if (q1.exec("SELECT * from zzz_test")) {
    24. while(q1.next()) {
    25. // QSqlQuery q2(db); // fails less badly here because it is destroyed at the end of the loop
    26. qDebug() << "Before q2" << q1.record() << q1.lastError().text();
    27.  
    28. // Any failing query
    29. if (!q2.exec("select * from zzz_nonexistent"))
    30. qDebug() << "q2 failed" << q2.lastError().text();
    31. // q2.clear(); // Adding this fixes the next line
    32.  
    33. qDebug() << "After q2" << q1.record() << q1.lastError().text();
    34. }
    35. }
    36.  
    37. db.exec("DROP TABLE zzz_test");
    38. }
    39.  
    40. return 0;
    41. }
    To copy to clipboard, switch view to plain text mode 

    I'd be inclined to raise a bug.

Similar Threads

  1. Replies: 6
    Last Post: 5th May 2010, 23:25
  2. Replies: 3
    Last Post: 12th December 2009, 08:51
  3. QDir entryList performing slowly
    By bunjee in forum Qt Programming
    Replies: 3
    Last Post: 8th October 2009, 16:21
  4. Using qsqlrecord?
    By triperzonak in forum Qt Programming
    Replies: 2
    Last Post: 23rd July 2008, 11:04
  5. QSqlRecord becoming invalid
    By mikro in forum Newbie
    Replies: 5
    Last Post: 3rd October 2006, 18:00

Tags for this Thread

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.