Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QT5.9.1 connect MySQL5.7.18
Forum Updated to NodeBB v4.3 + New Features

QT5.9.1 connect MySQL5.7.18

Scheduled Pinned Locked Moved Solved General and Desktop
qsqldatabasemac os xmysqlconnection
12 Posts 3 Posters 4.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • LuAriaL Offline
    LuAriaL Offline
    LuAria
    wrote on last edited by
    #2

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

    1 Reply Last reply
    0
    • SlaneS Offline
      SlaneS Offline
      Slane
      wrote on last edited by Slane
      #3

      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

      LuAriaL 1 Reply Last reply
      0
      • LuAriaL LuAria

        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.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @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

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        LuAriaL 1 Reply Last reply
        1
        • SlaneS Slane

          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

          LuAriaL Offline
          LuAriaL Offline
          LuAria
          wrote on last edited by
          #5

          @Slane Thanks, I will have a try.

          1 Reply Last reply
          0
          • jsulmJ jsulm

            @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

            LuAriaL Offline
            LuAriaL Offline
            LuAria
            wrote on last edited by
            #6

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

            jsulmJ 1 Reply Last reply
            0
            • LuAriaL LuAria

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

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #7

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

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              LuAriaL 2 Replies Last reply
              1
              • jsulmJ jsulm

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

                LuAriaL Offline
                LuAriaL Offline
                LuAria
                wrote on last edited by
                #8

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

                jsulmJ 1 Reply Last reply
                0
                • LuAriaL LuAria

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

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by jsulm
                  #9

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

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  2
                  • jsulmJ jsulm

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

                    LuAriaL Offline
                    LuAriaL Offline
                    LuAria
                    wrote on last edited by
                    #10

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

                    jsulmJ 1 Reply Last reply
                    0
                    • LuAriaL LuAria

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

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by jsulm
                      #11

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

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      LuAriaL 1 Reply Last reply
                      0
                      • jsulmJ jsulm

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

                        LuAriaL Offline
                        LuAriaL Offline
                        LuAria
                        wrote on last edited by
                        #12

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

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved