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.2k 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.
  • L Offline
    L Offline
    LuAria
    wrote on 25 Aug 2017, 06:20 last edited by
    #2

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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Slane
      wrote on 25 Aug 2017, 07:48 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

      L 1 Reply Last reply 25 Aug 2017, 09:09
      0
      • L LuAria
        25 Aug 2017, 06:12

        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.

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 25 Aug 2017, 08:37 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

        L 1 Reply Last reply 25 Aug 2017, 09:17
        1
        • S Slane
          25 Aug 2017, 07:48

          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

          L Offline
          L Offline
          LuAria
          wrote on 25 Aug 2017, 09:09 last edited by
          #5

          @Slane Thanks, I will have a try.

          1 Reply Last reply
          0
          • J jsulm
            25 Aug 2017, 08:37

            @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

            L Offline
            L Offline
            LuAria
            wrote on 25 Aug 2017, 09:17 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.

            J 1 Reply Last reply 25 Aug 2017, 10:21
            0
            • L LuAria
              25 Aug 2017, 09:17

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

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 25 Aug 2017, 10:21 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

              L 2 Replies Last reply 25 Aug 2017, 12:30
              1
              • J jsulm
                25 Aug 2017, 10:21

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

                L Offline
                L Offline
                LuAria
                wrote on 25 Aug 2017, 12:30 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!

                J 1 Reply Last reply 25 Aug 2017, 12:37
                0
                • L LuAria
                  25 Aug 2017, 12:30

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

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 25 Aug 2017, 12:37 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
                  • J jsulm
                    25 Aug 2017, 10:21

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

                    L Offline
                    L Offline
                    LuAria
                    wrote on 25 Aug 2017, 12:38 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?

                    J 1 Reply Last reply 25 Aug 2017, 12:40
                    0
                    • L LuAria
                      25 Aug 2017, 12:38

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

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 25 Aug 2017, 12:40 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

                      L 1 Reply Last reply 25 Aug 2017, 12:55
                      0
                      • J jsulm
                        25 Aug 2017, 12:40

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

                        L Offline
                        L Offline
                        LuAria
                        wrote on 25 Aug 2017, 12:55 last edited by
                        #12

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

                        1 Reply Last reply
                        0

                        11/12

                        25 Aug 2017, 12:40

                        • Login

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