PDA

View Full Version : Empty SQlite Database



papasmurf1212
14th April 2013, 21:35
I have stored my database in my project folder and made the necessary changes to my .pro file. I currently can open and access the database but it claims that there are no tables. The database, when opened outside of QT, has multiple tables and the queries execute properly. In QT I do the following to run a query:


qDebug() << "Open?" << db.isOpen() << endl;
qDebug() << "isVALID" << db.isValid() << endl;
qDebug() << "SIZE" << db.tables().size() << endl;

QSqlQuery query("SELECT * FROM shifts", db);
if(!query.exec())
{
qDebug() << query.lastError().text();
qDebug() << "ERROR" << endl;
return;
}
while(query.next())
{

QString name = query.value(1).toString();

qDebug() << name << endl;

}

My output says the database is open, valid, and size is 0. Also, the variable db is of type QSqlDatabase and is established in the constructor of my class. Why does it assume the database is empty? Any suggestions on what is going wrong?

ChrisW67
15th April 2013, 01:23
You are not opening the database you think you are. Sqlite will happily 'open' a non-existent or zero-byte file and treat it as a database with no tables. Execute "PRAGMA database_list;" as Sql to see exactly what file is open by the Qt program (third column). If there is no file name then you have a ":memory:" database.

papasmurf1212
15th April 2013, 01:53
Thank you! You were right! Although I put my sqlite database in my project folder and added it accordingly to my .pro file, the file path was still wrong and thus it was opening a different database than I thought. However I now have the file path hardcoded, which is a poor solution as I am looking to deploy this application (obviously everyone's file paths will be different). Any suggestion on where to put the database so it can be loaded once the application starts up? It does not appear to work if I add it as a "Resource"

ChrisW67
15th April 2013, 11:36
If it is read-only then put it next to the program executable and use QCoreApplication::applicationDirPath(). For development ONLY you can place a writable database here too. If it is to be read-write then one of the locations available through QDesktopServices::storageLocation() (Qt4) or QStandardPaths (Qt5) is where it should be ultimately.