April 28, 2012

max2g max2g
Lab Rat
3 posts

SQLite returns result for sqlite_master but not for a user table ??????????

 

Hi all !

This is not a new problem but answers from different places never get me a workink solution ! So…

QT = 4.8.0
IDE = QT Creator with MINGW
OS = Windows XP (and I tried on Win 7 also and on Ubuntu !!!)

The problem :

I try to select some data in a table from a SQLite DB. If I tried to query the sqlite_master table, it does work ! However with user table named pkserial, it does not ? Why ????????

The project file is :

  1. #-------------------------------------------------
  2. #
  3. # Project created by QtCreator 2012-04-27T13:17:26
  4. #
  5. #-------------------------------------------------
  6.  
  7. QT       += core gui sql
  8.  
  9. TARGET = TestMySQL
  10. TEMPLATE = app
  11.  
  12.  
  13. SOURCES += main.cpp\
  14.         mainwindow.cpp
  15.  
  16. HEADERS  += mainwindow.h
  17.  
  18. FORMS    += mainwindow.ui

The code :

  1. void MainWindow::toolButtonClicked()
  2. {
  3.  qDebug() << "drivers :" << QSqlDatabase::drivers();
  4.  QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
  5.  
  6.  QString dbFile = "C:/Documents and Settings/Jack/My Documents/assmehari2010-v1a";
  7.  qDebug() << dbFile << QFileInfo(dbFile).exists();
  8.  db.setDatabaseName(dbFile);
  9.  
  10.  if (!db.open())
  11.  {
  12.   qDebug() << "unable to open db :" << db.lastError().text();
  13.   return;
  14.  }
  15.  
  16.  qDebug() << "tables :" << db.tables();
  17.  QSqlQuery query(db);
  18.  
  19.  // Query 1
  20.  QString sql = QLatin1String("SELECT tableName FROM pkserial");
  21.  
  22.  // Query 2
  23.  // QString sql = QLatin1String("SELECT name FROM sqlite_master WHERE type='table' OR type='view' "
  24.  //           "UNION ALL SELECT name FROM sqlite_temp_master WHERE type='table' OR type='view'");
  25.  
  26.  if (query.exec(sql))
  27.  {
  28.   while(query.next())
  29.    qDebug() << query.value(0).toString();
  30.  } else
  31.   qDebug() << "error :" << query.lastError().text();
  32.      
  33.  qDebug() << "End of test";
  34. }

When the code is executed, the result is :

  1. Starting C:\...\TestMySQL-build-desktop-Qt_Commercial_4_8_0_for_Desktop__MinGW__Debug\debug\TestMySQL.exe...
  2. drivers : ("QSQLITE", "QODBC3", "QODBC")
  3. "C:/Documents and Settings/Jack/My Documents/assmehari2010-v1a" true
  4. tables : ("pkserial")
  5. End of test
  6. C:\...\TestMySQL-build-desktop-Qt_Commercial_4_8_0_for_Desktop__MinGW__Debug\debug\TestMySQL.exe exited with code 0

If I comment query 1 and uncomment query 2, the result is :

  1. Starting C:\...\TestMySQL-build-desktop-Qt_Commercial_4_8_0_for_Desktop__MinGW__Debug\debug\TestMySQL.exe...
  2. drivers : ("QSQLITE", "QODBC3", "QODBC")
  3. "C:/Documents and Settings/Jack/My Documents/assmehari2010-v1a" true
  4. tables : ("pkserial")
  5. "pkserial"
  6. End of test
  7. C:\...\TestMySQL-build-desktop-Qt_Commercial_4_8_0_for_Desktop__MinGW__Debug\debug\TestMySQL.exe exited with code 0

You can see that line 5 shows the name of the table that was retrieved by the sql query 2 !

Therefore, whenever I query the catalog table, it works and when I query a user table, it does not ???????

Does anyone have a clue on what is happening ?

Thank you !

3 replies

April 28, 2012

alexisdm alexisdm
Ant Farmer
123 posts

The table pkserial might be empty. That wouldn’t be an error: query.exec() would return true, but query.next() would return false.

April 28, 2012

max2g max2g
Lab Rat
3 posts

Hi ! Thank you for your reply ! Before beginning this thread, I did tested that case and I insure that the table pkserial was not empty !

Here is the content of that table as seen in SQLite Maestro:

  1. tableName lastSerial fkProject
  2. GClassif 389 1
  3. GCodes 4953 1
  4. GExpo 3016 1
  5. GGravite 16 1
  6. GGroupeTypeActif 30 1
  7. GProcessus 0 1
  8. GProcessusTypeActif 0 1
  9. GProject 3 1
  10. GQuestions 2905 1

This table has three columns : tableName, lastSerial and fkProject. The first row gives the title and roow 2 up to the end gives each record.

So the problem remain unresolved !

Thank you to continue searching for a solution !

April 28, 2012

max2g max2g
Lab Rat
3 posts

Solved !

The database used to save the data has been build using SQL Maestro. In that case, the data inserted in the table is not returned when a Select is done on the table.

If I used SQLite Expert to create the database, everything works fine, that is, the data can be selected and is accessed through the query in my Qt Application.

Concluison : The tool used to create the database is the cause of the problem ! This means that you must take care of what tool you use to create the database. Moreover, I found that if I create (using my Qt application) a table X in a database Z created with SQL Maestro, than the data for that table X is returned as expected. But the other tables within that database Z are not !!!!!!!!!

 
  ‹‹ QMetaObject::invokeMethod doesn’t work when...      multiple colors in a single rectitem ››

You must log in to post a reply. Not a member yet? Register here!