Antonio Dias
Antonio Dias

Reputation: 2881

Why can't I delete the SQLite database in Qt even after I call the database close function?

I'm writing a code to work with a SQLite database, all the code of the database works, but at end, when I try to delete the database, the database file is not deleted. I've called the close function, so why can't I delete the database? The db.isOpen(); function returns false, and the QFile::remove(path); returns false too. Note: On Windows Explorer i can delete the database only after closing the program window.

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
QString path = "path_to_db";
db.setDatabaseName(path);

if(db.open())
{
    qDebug() << "Opened";
    qDebug() << path;
}
else
{
    qDebug() << db.lastError();
    return a.exec();
}
QSqlQuery query(db);

//code to work with the database

db.close();
qDebug() << db.isOpen();
qDebug() << QFile::remove(path);

Upvotes: 1

Views: 3588

Answers (1)

Antonio Dias
Antonio Dias

Reputation: 2881

I found the solution, simply call query.clear(); before close the connection.

Upvotes: 4

Related Questions