Sorry, but what's the problem? What did you try so far? Just call QSqlDatabase::setDatabaseName() with a file name instead of ":memory:" and skip create table statements if they already exist.
Sorry, but what's the problem? What did you try so far? Just call QSqlDatabase::setDatabaseName() with a file name instead of ":memory:" and skip create table statements if they already exist.
J-P Nurmi
I did. I got a query error that says the table does not exist? I did however put the open database statement in the .cpp file where I was using it. Does this matter? it looked like this:
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("test.db");
Do you see anything wrong here(missing ":" or something, I noticed the connection.h uses a ":" in front and back of memory)? My database open statement returns successfully, but the query error states that it cannot find a table I know is in there - The query statement I am using works from the command line.
One note, this is the first reference to "test.db" I have in the project, meaning it is not included anywhere else, could this be something?
goes2Bob
Hmm, are you 100% sure test.db lies in current working directory? What does
output?Qt Code:
#include <QtDebug> ... qDebug() << db.tables();To copy to clipboard, switch view to plain text mode
J-P Nurmi
I looked last night at where the db file was and it is in my workspace. By workspace, I mean that it is the /<proj name>/release directory (where I am running the executable). I also added the qDebug statement and it came back as "()" - I am guessing no tables, which explains why it cannot find the table. I was looking at the QSqlDatabase docs and found an example for an MS Access db statement which looked like this:
Qt Code:
db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=myaccessfile.mdb");To copy to clipboard, switch view to plain text mode
does there have to be anything in the "setDatabaseName" statement telling the application that the file is external? Remember as I stated in previous post that I have not included the external file in any way. The first time the application sees the filename is in the setDatabaseName statement. Should it be included in the .pro file? or as an extern?
Thanks in advance again,
goes2Bob
No, that's MS Access specific - SQLite takes the file name. I still suspect it's a problem with relative paths and that the current working directory is not the same where .db file is located.
Since you have test.db next to application executable, you could try:
Qt Code:
db.setDatabaseName(dir.filePath("test.db")); qDebug() << db.tables();To copy to clipboard, switch view to plain text mode
J-P Nurmi
You can also check if database exists using QFile::exists or QFileInfo::exists.
Success !! adding the "setDatabaseName(dir.filePath("test.db")); " allowed the app to find the file - and queries work just as they should.
This begs the question: why was that path direction necessary?
I am using the Eclipse IDE, Qt 4.2, on WinXP.
Thanks for your patience,
goes2Bob
Consider the difference between:
andC:\myapp> release\myapp.exe
When launching the application in first way, current working directory is "C:\myapp" whereas in the latter way it's "C:\myapp\release". Windows IDEs tend to launch the application from project dir, not from release or debug subdirectory.C:\myapp\release> myapp.exe
J-P Nurmi
Bookmarks