[Interest] how to properly close a SQLite database?

Francisco Ares frares at gmail.com
Tue Jun 24 19:25:40 CEST 2014


Hi,

Before of anything else, thanks to anyone reading this message.  I'm using
Qt 4.8.5 and QtCreator.

I use SQLite for data storage in an embedded system, and that is OK.  There
is a class, named "CDataBase" that holds the "QSqlDatabase" object and all
of the "QSqlQuery" objects used on this program, and all access to the
database itself is made through member functions of this class, providing a
non-SQL interface for the rest of the program. The class has an "init"
function to receive the database file name:

void CDataBase::init(QString fileName)
{
    mDB = QSqlDatabase::addDatabase("QSQLITE");
    mDB.setDatabaseName(fileName);
    mDB.open();
     ...
}

Now I need to implement a backup for that data in a removable drive. For
that, I have created a second "CDataBase" object to handle the backup data
base file. The objects that use "CDataBase" have means implemented to
switch from a "CDataBase" object to another one (at least I believe so, as
I could not test it).

When the program tries open the new object's database connection, the
program stops (in debug mode) on the first instruction of the "init"
function; hitting F10, a warning shows up on the program output pane:

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

So, I've implemented a "finish()" member function for this class:

void CDataBase::finish()
{
    if (mDB.isOpen())
    {
        mDB.commit();
        mDB.close();
        mDB.removeDatabase(mDB.connectionName());
    }
}

So, when I try this:

    mDataBaseWork->finish();


the same warning message shows up on trying to execute the "removeDatabase"
instruction.

All the QSqlQueries are temporary, they are all dead at this point. I even
tried to do:

{
    QSqlQuery query (mDB, query);
    ...
    ...
    query.finish();
    query.clear();
}

And it didn't work as well.

What am I doing wrong?

Thanks again,
Francisco
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20140624/5485b707/attachment.html>


More information about the Interest mailing list