Important: Please read the Qt Code of Conduct -

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");

    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");

    and it's may work

  • Lifetime Qt Champion

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

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

  • Lifetime Qt Champion

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

  • Lifetime Qt Champion

    @LuAria Did you read the link I posted?
    I even copy/pasted the line to get the connection:

    QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
    bool ok =;
    // 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");
        bool ok =;
        MainWindow *mainWindow = new MainWindow();
        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?

  • Lifetime Qt Champion

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