Results 1 to 3 of 3

Thread: Why does QSqlQuery::prepare() return false?

  1. #1
    Join Date
    Feb 2010
    Posts
    5
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Why does QSqlQuery::prepare() return false?

    Using: Qt3, Ubuntu 10.04, MySql 5.1

    I'm attempting to use a prepared query to update a mysql database. The prepare() command is contained in a function (importBinaryStateData()) which is part of a very large application (too large to post here, I'll call the main application). For some mysterious reason, when used in the main application, prepare() returns false, and of course fails to prepare the query. If, however, I copy the function (importBinaryStateData()) out to a stand-alone test application (attached), it works exactly as it should. The main and test applications use the same database, driver, user, etc. Also, there are numerous queries throughout the main application that work, including prepared queries.

    So I guess the question is this, is there any way to tell why prepare() might return false? I've tried lastError() and lastQuery(), all I get are nulls, zeros, and empty strings.

    importBinaryStateData() (including prepare()) works here, but not as part of my main app.

    Qt Code:
    1. #include <qapplication.h>
    2. #include <qsqldriver.h>
    3. #include <qsqldatabase.h>
    4. #include <qsqlquery.h>
    5. #include <qsqlcursor.h>
    6. #include <qfile.h>
    7.  
    8. #define DRIVER "QMYSQL3" /* see the Qt SQL documentation for a list of available drivers */
    9. #define DATABASE "Sequences" /* the name of your database */
    10. #define USER "user" /* user name with appropriate rights */
    11. #define PASSWORD "password" /* password for USER */
    12. #define HOST "the-server" /* host on which the database is running */
    13.  
    14. bool importBinaryStateData( QString, QString, QString, QString, int );
    15.  
    16. int main( int argc, char ** argv )
    17. {
    18.  
    19. QApplication a( argc, argv, FALSE );
    20. db = QSqlDatabase::addDatabase( DRIVER );
    21. db->setDatabaseName( DATABASE );
    22. db->setUserName( USER );
    23. db->setPassword( PASSWORD );
    24. db->setHostName( HOST );
    25. QString field = argv[1]; //"SM_Indx";
    26. QString oldId = argv[2]; //"1";
    27. QString newId = argv[3]; //"25";
    28. QString path = argv[4]; // "/home/me/workarea/sequences/SI-003";
    29. QString strSize = argv[5]; // 51497638
    30. int size = strSize.toInt();
    31. if (!importBinaryStateData(field, oldId, newId, path, size))
    32. return 0;
    33. else
    34. return 1;
    35. }
    36.  
    37. bool
    38. importBinaryStateData( QString field, QString oldId, QString newId, QString path, int size )
    39. {
    40. qDebug("importBinaryStateData()");
    41. bool success = true;
    42. if (size > 0)
    43. {
    44. QFile file (path + "/" + "States_" + oldId + "_" + field + ".bin");
    45. if (file.open(IO_ReadOnly))
    46. {
    47. QByteArray data = file.readAll();
    48. if (data.size() == size)
    49. {
    50. if (db->open())
    51. {
    52. QString query = "UPDATE States SET " + field + " = ? WHERE State_ID = " + newId;
    53. if (!q.prepare(query))
    54. {
    55. qDebug("prepare() failed!");
    56. }
    57.  
    58. q.bindValue(0, data);
    59. if (!q.exec())
    60. {
    61. qDebug("importBinaryActionData(): %s", q.lastError().text().ascii());
    62. success = false;
    63. }
    64. db->close();
    65. }
    66. else
    67. {
    68. qDebug("importBinaryActionData(): Database not open by calling procedure.");
    69. success = false;
    70. }
    71. }
    72. else
    73. {
    74. qDebug("importBinaryActionData(): Could not read %s", file.name().ascii());
    75. success = false;
    76. }
    77. file.close();
    78. }
    79. else
    80. {
    81. qDebug("importBinaryActionData(): Could not open %s", file.name().ascii());
    82. success = false;
    83. }
    84. }
    85. return success;
    86. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2010
    Posts
    5
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Why does QSqlQuery::prepare() return false?

    OK, figured it out ... sort of. I still don't know how to find out why QSqlQuery:repare() returns false. However, through trial, error, and ultimately someone who knew what they were doing, I did manage to figure out why it returns false in my particular case.

    The error is where I instantiate a QSqlQuery, q. This line should be

    Qt Code:
    1. QSqlQuery q(db);
    To copy to clipboard, switch view to plain text mode 

    As it turns out, when the database is not specified, there is, or may be, a default database. In the case of my test app, there was only one database, and by default it was the right one (hence, q.prepare() worked). In my main application however, there were multiple databases, and the one I was intending to interact with, was not the default one (hence, q.prepare() did not work).

    I still don't know the answer to my original question, but my immediate problem is fixed. Also, I might note that the offending line was lifted directly from the qt3 examples ... for whatever thats worth.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why does QSqlQuery::prepare() return false?

    Prepare returns false because you're trying to run it without connecting to the database first.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QSqlQuery::prepare() - placeholder as table name
    By dawwin in forum Qt Programming
    Replies: 2
    Last Post: 7th March 2011, 21:40
  2. qdbus connect return always FALSE
    By ciberkids in forum Qt Programming
    Replies: 4
    Last Post: 17th September 2009, 14:17
  3. QSqlQuery prepare() bindValue() broken badly
    By RolandHughes in forum Qt Programming
    Replies: 4
    Last Post: 14th November 2008, 18:25
  4. QSqlQuery::isValid returns false
    By mismael85 in forum Qt Programming
    Replies: 24
    Last Post: 7th September 2008, 23:43
  5. Database and QSqlQuery::prepare problem
    By fengtian.we in forum Qt Programming
    Replies: 1
    Last Post: 31st October 2007, 23:17

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.