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 that

    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.

    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.



  • What's more, I never used the sentence removeDatabase



  • 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


  • Moderators

    @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



  • @Slane Thanks, I will have a try.



  • @jsulm Sorry it's my mistake. Maybe I cannot express myself very well.
    I wrote many cpps on one project. And I wrote that code on even every cpp.
    So I meant whether I wrote that code so many times led to the warning tip.


  • Moderators

    @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!


  • Moderators

    @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;
    }
    


  • @jsulm
    I mean should I initialize same database connection once on one cpp, and then when I need using, I can only use the code
    QSqlDatabase db = QSqlDatabase::database("QMYSQL", "connectionName");
    to operate on the database?


  • Moderators

    @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.



  • @jsulm Thanks a lot!
    Sorry for my miss on the link.
    I will try it.



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.