PDA

View Full Version : QSQLITE problem with DROP



protagonista3
18th March 2012, 18:27
Hi to all,

I have a problem with a db software that I create for my friend. The problem is the following:

When I do:



QSqlQuery *qry = new QSqlQuery();

qry->prepare("DROP TABLE Month");

if(!qry->exec()){

qDebug() << qry->lastError().text();

}



I get the following error during the application runtime:

"database table is locked Unable to fetch row"

How can I solve this problem? I try to Delete the table with sqlite_browser but after that I re-create the table Month with:



QSqlQuery *qry1 = new QSqlQuery();

if(!qry1->exec("CREATE TABLE IF NOT EXISTS Month (Codice NUMERIC, Prodotto TEXT, Qta TEXT)"))

qDebug() << qry1->lastError().text();

delete qry1;



and try to DROP the table Month, I obtain the same error.

My db is in qsqlite.


Sorry for my bad english but I'm italian and I hope that we can understand my really bad english

Thanks a lot in advance !!!!

pdoria
18th March 2012, 23:08
Since SQLite databases are mere files... have you checked file permissions on it? (i.e. not read-only)

Usually if a process has a lock on a sqlite database and crashes it locks it permanently. If that's the case I suggest dump and restore it to another file... ;)

Linux solution:

echo ".dump" | sqlite old.db | sqlite new.db

HTH

ChrisW67
18th March 2012, 23:53
If the directory containing the database cannot be written then Sqlite cannot create needed temporary files to track transactions and you get messages similar to this. On Windows writes will be blocked by default if you place the database in a subfolder of Program Files hierarchy.

protagonista3
19th March 2012, 11:48
I'm under Windows. To see if the problem is the db, I create another program to do the Drop. The new software do it without problem with the same db. But when I try my software I obtain the error.

My db is in C:\Users\Luigi\Documents\Qt_Code\Database\Prodotti .db

It is possible that I forgot to delete an QSqlQuery's object?

I check but I see that all my QSqlQuery's objects are deleted.

Added after 5 minutes:

And after this problem when I close the application I get the warning:

QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.

But When I click the pushButton to Close the app I execute:




db.close();
this->close();



where db is a QSqlDatabase object.

ChrisW67
19th March 2012, 23:24
Are there multiple copies of your application running?
Or are there multiple threads in your application holding connections to the database?
Or is sql_browser still running and holding a lock?

protagonista3
22nd March 2012, 21:27
There aren't applications when I run my app and don't uses multiple thread.

Added after 1 11 minutes:

I solve the warning:

QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.

To solve this I delete the QSqlTableModel's Object.

But I cannot solve

"database table is locked Unable to fetch row"

I try to create a new db but the problem still remains.

ChrisW67
22nd March 2012, 21:30
Is the file "C:\Users\Luigi\Documents\Qt_Code\Database\Prodotti .db" writeable?
Can you drop tables using the sqlite shell (http://sqlite.org/download.html) command line tool?
Do you execute any "pragma (http://sqlite.org/pragma.html)" commands on your connection?
Does your database pass a "pragma integrity_check; (http://sqlite.org/pragma.html#pragma_integrity_check)" ?

Provide a minimal, compilable example that fails on your machine on a file in that directory.

protagonista3
22nd March 2012, 21:50
Finally I solve the last warning.

The problem is the QSqlTableModel that locks the functions DROP and ALTER. Infact when I do model->clear(); or delete model; before the query, I can do everything.

Do you know the reasone?