Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

warning remove database

Tags:

database

mysql

qt

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.

like image 841
Jjreina Avatar asked Mar 01 '12 16:03

Jjreina


3 Answers

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);
}
like image 104
alexisdm Avatar answered Nov 20 '22 18:11

alexisdm


void database::close()
{
    QString connection;
    connection = m_db.connectionName();
    m_db = QSQlDatabase();
    //m_db.close();
    m_db.removeDatabase(connection);
}

try this it will work..

like image 5
shofee Avatar answered Nov 20 '22 16:11

shofee


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;
like image 1
matto Avatar answered Nov 20 '22 18:11

matto