Environment: windows10 + MySQL + Qt5 nine point nine
A few months ago, a project involved in development needed to connect to the same database many times for operation, but the following errors were found during execution:
QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
In fact, the previous contact is to operate a database, so it is relatively simple; But this time, the same database was connected in different places for a series of operations, so it was processed according to the previous idea. Indeed, there was an error. It was solved after looking for it all afternoon. Now the relevant series are posted, which may help you!!!
1. The following code is the key code (the initialization connection and other parts are ignored)
Segment 1:
bool Database::init() { static bool isFirst = true; m_DBName = QString("Alarm_Center"); //database m_DBpassword = QString("lxgs123"); m_db = QSqlDatabase::addDatabase("QMYSQL"); //***1 m_db.setHostName("10.24.3.239"); m_db.setUserName("root"); m_db.setPassword(m_DBpassword); m_db.setDatabaseName(m_DBName); if(!m_db.open()){ return false; } return true; }
Segment 2:
bool Database::initForPie() { static bool isFirstForPie = true; m_DBNameForPie = QString("Alarm_Center"); //database m_DBpasswordForPie = QString("lxgs123"); m_db_ForPie = QSqlDatabase::addDatabase("QMYSQL"); //***2 m_db_ForPie.setHostName("10.24.3.239"); m_db_ForPie.setUserName("root"); m_db_ForPie.setPassword(m_DBpasswordForPie); m_db_ForPie.setDatabaseName(m_DBNameForPie); if(!m_db_ForPie.open()){ return false; } return true; }
Let's take a look at the init() function in the code in fragment 1. We connected the database alarm in the init() function_ Center; There is no problem, and we can operate directly in the future;
Next, I connected the database alarm in the initForPie() function because of the need_ Center, but the above error will occur,
What is the reason???
After checking, [static] QSqlDatabase QSqlDatabase:: adddatabase (const qstring & type, const qstring & connectionName = qlatin1string (defaultconnection))
The second parameter of addDatabase() function is the connection name. If we only connect to the database once, it doesn't matter. However, if we want to connect to the database again in other places, specify the second parameter (connection name) when calling addDatabase() function;
2. Solutions
Specify the second parameter connection name for addDatabase() function at the position marked with / / * * * 1 and / / * * * 2 in fragment 1 and fragment 2 (that is, the calling function of addDatabase() function). At this time, you can manipulate the same database multiple times in different places!
Therefore, the revised codes are respectively (see if... else if... For the modified code):
Segment 1:
bool Database::init() { static bool isFirst = true; m_DBName = QString("Alarm_Center"); m_DBpassword = QString("lxgs123"); if(isFirst) { m_db = QSqlDatabase::addDatabase("QMYSQL","first"); //Connection name specified isFirst = false; } else if(QSqlDatabase::contains("first")) { m_db = QSqlDatabase::database("first"); } m_db.setHostName("10.24.3.239"); m_db.setUserName("root"); m_db.setPassword(m_DBpassword); m_db.setDatabaseName(m_DBName); if(!m_db.open()){ return false; } return true; }
Segment 2:
bool Database::initForPie() { static bool isFirstForPie = true; m_DBNameForPie = QString("Alarm_Center"); m_DBpasswordForPie = QString("lxgs123"); if(isFirstForPie) { m_db_ForPie = QSqlDatabase::addDatabase("QMYSQL", "DBForPieSeries");//Connection name specified isFirstForPie = false; } else if(QSqlDatabase::contains("DBForPieSeries")) { m_db_ForPie = QSqlDatabase::database("DBForPieSeries"); } m_db_ForPie.setHostName("10.24.3.239"); m_db_ForPie.setUserName("root"); m_db_ForPie.setPassword(m_DBpasswordForPie); m_db_ForPie.setDatabaseName(m_DBNameForPie); if(!m_db_ForPie.open()){ return false; } return true; }
At this time, everything is normal. You can connect to the same database many times in different places!!!