Fork me on GitHub

QSqlTableModel Error

A quick post about a pesky error. Thought this error wasn't interfering with my code on the surface, I'd rather take no chances. Here's the code causing the error:

userdb.open();
m_observerList.clear();
QSqlTableModel users(0,userdb);
users.setTable("user");
users.setFilter("id >= 1");
users.select();
/* some foo here */
userdb.close();
kWarning() << userdb.lastError;

And here's the error:

QSqlError(-1, "Error closing database", "unable to close due to unfinalised statements")

The problem seems to be that even though we ask userdb to close(), users is still connected to that query it generated. For other libraries, the solution is to 'finalize' the query somehow. The solution for Qt seems to be:

/* some foo here */
users.clear();  //clear 'users'!!
userdb.close();
kWarning() << lastError();

The documentation, for some strange reason, says we would need to use clear() rarely.

~~Update~~ This is different than the issue where any object dealing with the databases must be 'destroyed' (not simply reallocated) or it won't remove the database. i.e. That issue is for removing the database, not closing it.

Comments !

blogroll

social