Results 1 to 15 of 15

Thread: QSqlQuery problem

  1. #1
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default QSqlQuery problem

    Hi to all!

    Here is the code:
    Qt Code:
    1. QString queryString("SELECT * from merchandize WHERE GrupaIdentificationNumber=%1;");
    2. //QString queryString("SELECT * from merchandize WHERE GrupaIdentificationNumber=2;"); // test
    3. queryString=queryString.arg(groupId);
    4. qDebug() << "Query: " << queryString; // debug
    5.  
    6. QSqlQuery query(queryString); // sets up query from query string
    7. removeAllMerchandize(); // removes all items
    8. m_iImageCounter=0; // inits counter
    9. //query.exec(queryString); // executes query string
    10. while (query.next())
    11. {
    12. .....
    13. }
    To copy to clipboard, switch view to plain text mode 

    As you can see, I've debugged created query and it is ok. But why the program flow skips while loop. The database is filled and the application is connected to it as it must be. I thought there is something wrong with query itself, but as I copied quey, which was produces with qDebug() and pasted it into mysql by hand, I got right results. Can someone help me please?
    Qt 5.3 Opensource & Creator 3.1.2

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery problem

    What does QSqlQuery::isActive() return just before the loop?

  3. #3
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QSqlQuery problem

    God damn, I've recoded the code using isActive like that:
    Qt Code:
    1. void CMerchandizeBrowser::fillMerchandize(int groupId)
    2. {
    3. qDebug() << "Entered CMerchandizeBrowser::fillMerchandize ..."; // debug
    4. // sets up query string
    5. QString queryString("SELECT * from merchandize WHERE GrupaIdentificationNumber=%1;");
    6. //QString queryString("SELECT * from merchandize WHERE GrupaIdentificationNumber=2;"); // test
    7. queryString=queryString.arg(groupId);
    8. qDebug() << "Query: " << queryString; // debug
    9.  
    10. QSqlQuery query(queryString); // sets up query from query string
    11. removeAllMerchandize(); // removes all items
    12. m_iImageCounter=0; // inits counter
    13. //query.exec(queryString); // executes query string
    14. // WARNING: Are we connected to database????
    15. if (query.isActive())
    16. {
    17. while (query.next())
    18. {
    19. /*
    20.   if (groupId==query.value(iMerchandizeFieldGROUPID).toInt())
    21.   {
    22.   */
    23. QImage img(query.value(iMerchandizeFieldPICPATH).toString()); // fetches image from filename
    24.  
    25. tEmbeddImagesResult result=embeddedPicPath(img, query.value(iMerchandizeFieldPICPATH).toString()); // embedd pic path into pic
    26. if (result!=resFailue)
    27. {
    28. img=img.convertToFormat(QImage::Format_ARGB32_Premultiplied);
    29. }
    30. //addMerchandize(query.value(iMerchandizeFieldPICPATH).toString()); // adds merchandize pics
    31. setSlideCount(m_iImageCounter+1);
    32. setSlide(m_iImageCounter, img);
    33. m_iImageCounter++;
    34. //}
    35. } // while
    36. };
    37. m_iSelected=m_iImageCounter / 2; // sets selected pic
    38.  
    39. //update(); // updates window
    40. qDebug() << "m_iImageCounter: " << m_iImageCounter; // debug
    41. qDebug() << "m_iSelected: " << m_iSelected; // debug
    42. qDebug() << "Exiting CMerchandizeBrowser::fillMerchandize ..."; // debug
    43. }
    To copy to clipboard, switch view to plain text mode 

    and isActive under if skips while loop. Thas means I am not connected to database??
    Qt 5.3 Opensource & Creator 3.1.2

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery problem

    Quote Originally Posted by MarkoSan View Post
    Thas means I am not connected to database??
    Not necessarily. What does QSqlQuery::lastError() return?

  5. The following user says thank you to jacek for this useful post:

    MarkoSan (14th January 2008)

  6. #5
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QSqlQuery problem

    God damn, the line
    Qt Code:
    1. qDebug() << query.lastError().text(); // debug
    To copy to clipboard, switch view to plain text mode 
    returns
    Qt Code:
    1. warning: "No database selected QMYSQL: Unable to execute query"
    To copy to clipboard, switch view to plain text mode 
    . Jacek, thanks for hints, but clear this up for me please. How is it possible that in the same object and in other method, one time the database is active and the second time, like in this problem, it goes offline. Is there any timeout setting for MySQL database???
    Qt 5.3 Opensource & Creator 3.1.2

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery problem

    What happens in removeAllMerchandize()?

  8. #7
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QSqlQuery problem

    Here is code, nothing special:
    Qt Code:
    1. void CMerchandizeBrowser::removeAllMerchandize()
    2. {
    3. qDebug() << "Entered CMerchandizeBrowser::removeAllMerchandize ..."; // debug
    4. // qDebug() << "Number of pics: " << m_ImagesList.count();
    5. // for(qint16 iIndex=0; iIndex<m_ImagesList.count(); iIndex++)
    6. // {
    7. // m_ImagesList.removeAt(iIndex); // removes item
    8. // }
    9. //clear(); // clear slides of all images
    10. d->slideImages.clear(); // clears intertnal pic presentation
    11. //update(); // updates screen
    12. qDebug() << "Exiting CMerchandizeBrowser::removeAllMerchandize ..."; // debug
    13. }
    To copy to clipboard, switch view to plain text mode 

    And d is private class under CMerchandizeBrowser. Let me comment it and then see what happens.
    Qt 5.3 Opensource & Creator 3.1.2

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery problem

    How do you connect to the database? Do you use a named connection?

  10. #9
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QSqlQuery problem

    I've coded database singleton class with your help, I think, a while ago and I am reusing it now in this application. Do you need a code?
    Qt 5.3 Opensource & Creator 3.1.2

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery problem

    No, I just want to know the line with addDatabase().

  12. #11
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QSqlQuery problem

    Ok, here is the whole constructor of my CDatabaseFoundation class:
    Qt Code:
    1. CDatabaseFoundation::CDatabaseFoundation(QString db_type, QString db_host,
    2. QString db_name, QString db_username,
    3. QString db_password): QObject()
    4. {
    5. // sets up database paramaters
    6. m_Database=QSqlDatabase::addDatabase(db_type);
    7. m_Database.setHostName(db_host);
    8. m_Database.setUserName(db_username);
    9. m_Database.setPassword(db_password);
    10.  
    11. // tries to connect to database
    12. m_bConnectionEstablished=m_Database.open();
    13. if(!m_bConnectionEstablished)
    14. qFatal("Failed to connect to database. Aborting.");
    15. //create database + tables if they don't exists yet
    16. //CreateDatabaseAndTables();
    17.  
    18. //call seletedDatabase statement here because you're sure the the database exists
    19. m_Database.setDatabaseName(db_name);
    20.  
    21.  
    22. /*
    23.   m_pModel=new QSqlRelationalTableModel(this);
    24.   Q_CHECK_PTR(m_pModel);
    25.   //all changes will be cached in the model until either submitAll() or revertAll() is called
    26.   m_pModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    27. */
    28. //addRelations(m_pModel);
    29.  
    30. }
    To copy to clipboard, switch view to plain text mode 
    Qt 5.3 Opensource & Creator 3.1.2

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery problem

    Quote Originally Posted by MarkoSan View Post
    // tries to connect to database
    m_bConnectionEstablished=m_Database.open();
    if(!m_bConnectionEstablished)
    qFatal("Failed to connect to database. Aborting.");
    //create database + tables if they don't exists yet
    //CreateDatabaseAndTables();

    //call seletedDatabase statement here because you're sure the the database exists
    m_Database.setDatabaseName(db_name);
    As the setDatabaseName() docs say: "This must be done before the connection is opened or it has no effect;". Put that line before the call to open().

  14. The following user says thank you to jacek for this useful post:

    MarkoSan (14th January 2008)

  15. #13
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QSqlQuery problem

    Ok, jacek, now query works fine. But please tell me, how is it possible that the SAME CODE worked before?????
    Qt 5.3 Opensource & Creator 3.1.2

  16. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery problem

    Quote Originally Posted by MarkoSan View Post
    how is it possible that the SAME CODE worked before?????
    Yesterday was the 13th.

  17. #15
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QSqlQuery problem

    Hmm, you can mock at me as much as you want, but in the same object other method works without problem, I tried it now. I have to solve this mistery, anyway, jacek, thank you very very much for your guidance.
    Qt 5.3 Opensource & Creator 3.1.2

Similar Threads

  1. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  2. QSqlQuery problem
    By MarkoSan in forum Qt Programming
    Replies: 11
    Last Post: 18th December 2007, 13:25
  3. QSqlQuery error
    By skuda in forum Qt Programming
    Replies: 2
    Last Post: 2nd November 2007, 08:43
  4. [QMYSQL] connection problem
    By chaos_theory in forum Installation and Deployment
    Replies: 5
    Last Post: 2nd July 2007, 09:52
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.