my code show this next warning:
QSqlDatabasePrivate::removeDatabase: connection ‘qt_sql_default_connection’
is still in use, all queries will cease to work
This is my code the connection with data base is fine:
QSqlDatabase database::db()
{
return m_db;
}
bool database::connect()
{
m_db = QSqlDatabase::addDatabase("QMYSQL");
m_db.setDatabaseName("aaaa");
m_db.setHostName("192.168.xxx.xxx");
m_db.setUserName("xx");
m_db.setPassword("xxxx");
m_db.setPort(1234);
return m_db.open();
}
void database::close()
{
QString connection;
connection = m_db.connectionName();
m_db.close();
m_db.removeDatabase(connection);
}
m_db is define as:
QSqlDatabase m_db;
and my test is:
database db;
qDebug() << "CONNECT: " << db.connect();
db.close();
How can I fix it?
Thanks you very much.
After you closed it, m_db
still holds a reference to the database you configured in connect()
.
You can reset m_db
by assigning a default constructed QSqlDatabase
:
void database::close()
{
QString connection;
connection = m_db.connectionName();
m_db.close();
m_db = QSqlDatabase();
m_db.removeDatabase(connection);
}
void database::close()
{
QString connection;
connection = m_db.connectionName();
m_db = QSQlDatabase();
//m_db.close();
m_db.removeDatabase(connection);
}
try this it will work..
Adding an additional scope does the same trick:
QString connectionName;
bool ok = false;
{
QSqlDatabase db = QSqlDatabase::addDatabase(databaseType);
connectionName = db.connectionName();
db.setHostName(hostname);
db.setDatabaseName(databaseName);
db.setUserName(userName);
db.setPassword(password);
ok = db.open();
db.close();
}
QSqlDatabase::removeDatabase(connectionName);
return ok;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With