QT5.9.1 connect MySQL5.7.18
-
I have finally used OT to connect MySQL successfully yesterday.
However, when I ran the project, it wraned me thatQSqlDatabasePrivate::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.I used
this->db = QSqlDatabase::addDatabase("QMYSQL");
this->db.setHostName("127.0.0.1");
this->db.setPort(3306);
this->db.setUserName("root");
this->db.setPassword("lulu168168");
this->db.setDatabaseName("student");in many places.
Is that led to the warning?
Thanks you. -
you need to create a deeper scoop
like :// deeper scoop { db = QSqlDatabase::addDatabase("QMYSQL","ConnectionName"); db.setHostName("127.0.0.1"); db.setPort(3306); db.setUserName("root"); db.setPassword("lulu168168"); db.setDatabaseName("student"); } QSqlDatabase::removeDatabase("ConnectionName");
and it's may work
-
@LuAria said in QT5.9.1 connect MySQL5.7.18:
in many places
Why? There is even no need to have this db variable. Just use the default connection and get it when needed using
QSqlDatabase db = QSqlDatabase::database();
And set up the database only once.
Currently you create many connections which is just waste of resources.
See http://doc.qt.io/qt-5/qsqldatabase.html -
@LuAria Yes, this is what I mean: there is no need to initialize same database connection several times in different places (different cpp files). Do it once and then get the connection where you need it like I shown.
The warnings you get are most probably caused by this problem: you have many active connections. -
@jsulm Thanks.
I think I get your point.
But how can I do it once and get the connection.(Is there a special code to get the connection?)
Should I define a global variable?
The other hand, should I use ~.close() and QSqlDatabase::database()
after using the database. I haven't use that two codes before?
Thank you very much! -
@LuAria Did you read the link I posted?
I even copy/pasted the line to get the connection:QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL"); db.setHostName("acidalia"); db.setDatabaseName("customdb"); db.setUserName("mojito"); db.setPassword("J0a1m8"); bool ok = db.open(); // Then somewhere else, where you need to use the connection QSqlDatabase db = QSqlDatabase::database();
No need for any global variable!
Where to initialize the connection and where to close it depends on your design. You can do it in main:int main() { QApplication app(...); QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL"); db.setHostName("acidalia"); db.setDatabaseName("customdb"); db.setUserName("mojito"); db.setPassword("J0a1m8"); bool ok = db.open(); MainWindow *mainWindow = new MainWindow(); mainWindow->show(); bool exitStatus = app.exec(); // Here close the connection return exitStatus; }
-
@LuAria Yes, this is what I'm saying.
To be sure: you do not have to initialize the connection in every cpp file! Just do it ONCE in your project.
You should really read the documentation which I mentioned before - it actually describes this.