PDA

View Full Version : How to execute VACUUM command on SQLITE db



Marmelade
6th June 2015, 20:21
Hello,

I can't run VACUUM on my database file.

I tried this:
QSqlQuery query;
query.exec("VACUUM");
But I get the following message:
cannot VACUUM - SQL statements in progress Unable to fetch rowDo you have an idea to solve this problem?

Thanks

ChrisW67
6th June 2015, 23:26
I assume your sqlite database is the default connection.

You cannot vacuum an sqlite database if there are open transactions or active sql statements in the database. These transactions may be in your program or from another process.

Look for other QSqlQuery or similar objects that are still open/active/in scope.

jefftee
7th June 2015, 08:24
Do you have an idea to solve this problem?
In addition to what ChrisW67 said, a QSqlQuery that you've prepared can cause this if it hasn't been finished.

Marmelade
7th June 2015, 09:40
My program is really simple, a kind of address book.
The VACUUM call is done in SLOT and I am certain at this point that there are no other running queries.

For this to work I found this:
QSqlDatabase db= QSqlDatabase::database();
db.close();
db.open();
QSqlQuery query;
query.exec("VACUUM");
Thank you for your advice

ChrisW67
7th June 2015, 22:07
How do you know the Vacuum command is not working?
Is the Sqlite database in a writeable folder/file.?

jefftee
7th June 2015, 23:29
You should post your actual code, the example above is not useful.

Marmelade
8th June 2015, 12:31
How do you know the Vacuum command is not working?
Is the Sqlite database in a writeable folder/file.?
File is in home directory.
You should post your actual code, the example above is not useful.
void WidgetServeurs::CompactBD() {
QSqlDatabase db= QSqlDatabase::database();
db.close();
db.open();
QSqlQuery query;
if(! query.exec("VACUUM"))
QMessageBox::critical(this, "", query.lastError().text().toStdString().c_str());
}I tried to replace close() and open() by commit() but I got the same error message.

wysota
8th June 2015, 12:57
Do you have any QSqlQuery, QSqlQueryModel or QSqlTableModel objects elsewhere in your program?

Marmelade
8th June 2015, 14:00
Yes, I have two QTableView with its associated QSqlTableModel and ten QSqlQuery in different methods.

wysota
8th June 2015, 16:05
Yes, I have two QTableView with its associated QSqlTableModel and ten QSqlQuery in different methods.

Are these models and objects destroyed before you try to vacuum the database?

Marmelade
8th June 2015, 18:49
QSqlQuery are destroyed because their scope is still limited.
But QTableView and QSqlTableModel are only destroyed at the exit in destructor.

wysota
8th June 2015, 22:35
So it seems you have open queries and vacuum doesn't work.