[Interest] how to properly close a SQLite database?
Constantin Makshin
cmakshin at gmail.com
Wed Jun 25 05:46:25 CEST 2014
QSqlDatabase::close() closes the database backend connection but leaves
QtSql one intact, i.e. even after close() your QSqlDatabase object
occupies its slot in the [global] list of existing database objects. So
in order to let QSqlDatabase::removeDatabase() do its job you have to
destroy all QSqlDatabase objects referring to that connection.
Here is a fragment from one of my applications:
void Application::closeDatabase ()
{
QString connectionName = m_db.connectionName();
m_db.close();
m_db = QSqlDatabase();
QSqlDatabase::removeDatabase(connectionName);
}
On 06/24/2014 09:25 PM, Francisco Ares wrote:
> 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 --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20140625/101b76aa/attachment.sig>
More information about the Interest
mailing list