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