Releasing database file with QSqlDatabase
Hi
I am struggling to release a database file I use with QSqlDatabase. I need the ability to close the connection and then delete the database file before replacing it with a new database. I can't delete it because the my program is holding onto it. I create my connection using the code below and dbProject is define globally for now.
Code:
bool createDBConnection() {
}
dbProject.setDatabaseName(active_db_file);
if (!dbProject.open()) {
// Error messages
}
return dbProject.isValid();
}
To release the file I am trying:
According to the QT docs it seems like this should be good enough. Does anybody know why it does not work?
Thanks
Jaco
Re: Releasing database file with QSqlDatabase
Re: Releasing database file with QSqlDatabase
What is the error you are getting? Something about queries still being active?
Re: Releasing database file with QSqlDatabase
Thanks for the replies.
I'll try ::removeDatabase() as well. Regarding an error message, I don't get any error message, the QFile remove() just fails since the file is opened by the program. I've verified this by trying to delete the file using an external application like windows explorer, which can't delete it because is being used by another application, my program.
I'll give removeDatabase() a go.
Re: Releasing database file with QSqlDatabase
Until you have released the database using removeDatabase() a lock is held onto the file, that's correct.
Re: Releasing database file with QSqlDatabase
No unfortunately that did not solve the problem. I've called removeDatabase() but it still keeps a lock on the file. To verify that I'm using it correctly,
If I add a database like this:
QSqlDatabase dbProject = QSqlDatabase::addDatabase("QSQLITE");
dbProject.setDatabaseName(active_db_file);
I remove it like this:
dbProject.close();
QSqlDatabase::removeDatabase(active_db_file);
Is this correct? If so there is something holding onto the database that I need to find...
Re: Releasing database file with QSqlDatabase
No, this is wrong. Call removeDatabase() without parameters.
Re: Releasing database file with QSqlDatabase
I tried that but it does not seem to work:
error: no matching function for call to 'QSqlDatabase::removeDatabase()'
note: candidates are: static void QSqlDatabase::removeDatabase(const QString&)
It needs a database name, but for this I've tried both active_db_file and "QSQLITE" without any effect. I've also tried to give a specific name to my connection but this does not seem possible.
Re: Releasing database file with QSqlDatabase
Blah, sorry. Should be:
or
if you use Qt older than Qt 4.4.
Re: Releasing database file with QSqlDatabase
Hi, thanks again for the reply.
I've tried your suggestions and checked to see if the connectionNames() list is empty after the command which is the case. Thus it removes the database. However it still keeps lock of the database. As I said before, I verify this by using windows explorer to try and delete it. Also, the QFile remove command fails on the file.
Any idea why the program is not losing the lock on the file?
Thanks
Jaco
Re: Releasing database file with QSqlDatabase
I'm bumping this thread. Has anyone found a way to resolve this problem as I am having the same issue. If not I'll have to delve into the source code.
Basically I am doing all of the above in order to release the lock on the file. I have even dynamically created QApplication and my main window which is then deleted after use in main() and then tried to delete the database file from there without success.
Anyone else have the same problem? It's only an issue on Windows as far as I can tell (ok under Linux).
Re: Releasing database file with QSqlDatabase
actually you shouldnt replace database file. you can just drop all database's tables, and voila u have new shiny and clean database.
Re: Releasing database file with QSqlDatabase
Use ":memory:" instead of file name. http://osdir.com/ml/db.sqlite.genera.../msg00257.html.
Or remove all instance of SQL objects.
Re: Releasing database file with QSqlDatabase
Ahhh yes. I should probably use the :memory: option which would make sense. Thankyou for the advice.
Regards,
Gareth.
Re: Releasing database file with QSqlDatabase
GoodMorning to everybody,
I have the same problem but... I can't use ":memory:" database.
I need to create one database for each test make on a special machine; I start a thread that manage the test, I create an sqlite db to store the data of the test and when the test end, I close the the db, zip the file and store it into MYSQL DB (blob field). Now the file sqlite is old, and I need to delete it at the end of the test, before create a new test.
There is no way to do this?
Thanks for your time
Michele
Re: Releasing database file with QSqlDatabase
Be sure you don't have any active queries. Execute finish() on any existing queries.
Re: Releasing database file with QSqlDatabase
Dear DanH,
No Way :( I added the finish() function before to close database connection, but no way, the file is still lock. These are my functions:
Code:
[...]
void myThread1::clearDBConn(){
qry->finish();
db.close();
}
void myThread1::zipDB(){
zipName=nomeDB;
clearDBConn();
archive(zipName.replace(".s3db",".zip"),nomeDB,true);
emit zipFileName(idProva, zipName);
//delName=QDir().absoluteFilePath(nomeDB);
}
Is there something wrong?
Thanks for your time
Michele
Re: Releasing database file with QSqlDatabase
Yes. You need to destroy the db object before calling removeDatabase().
Re: Releasing database file with QSqlDatabase
Thanks wysota but... I called
before removeDatabase(), so I've destroy the object isn't it?
Thanks for your time
Michele
Re: Releasing database file with QSqlDatabase
If you do that, the application will crash when you quit it. Why are you storing the db object in the first place?