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. The correct way to open mysql connection
Forum Update on Monday, May 27th 2025

The correct way to open mysql connection

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 993 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.
  • M Offline
    M Offline
    madababi
    wrote on 17 Oct 2016, 14:41 last edited by
    #1

    Hi,
    My Qt application need to connect to MySQL database to retrieve or add data.
    In my main class, I create the connection (only one time) using this code:

    QSqlDatabase base = QSqlDatabase::addDatabase("QMYSQL");
        base.setHostName("");
        base.setUserName("");
        base.setPassword("");
        base.setDatabaseName("");
        if(!base.open(){
            baseOpened=false;//boolean
        }
        else{        baseOpened=true;}
        base.close();
        base = QSqlDatabase();
    

    After that, when i need to retreive data I use this code

    QSqlDatabase db = QSqlDatabase::database();
        QSqlQuery query;
        if(db.isOpen())
        {
            query.exec("SELECT * ...");
            ...
            db.close();
        }
    

    the problem is that when i let the application open for longtime i get an error "too many open files"

    J 1 Reply Last reply 18 Oct 2016, 04:21
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 17 Oct 2016, 21:46 last edited by
      #2

      Hi,

      Why are you closing the connection each time ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply 18 Oct 2016, 07:14
      0
      • M madababi
        17 Oct 2016, 14:41

        Hi,
        My Qt application need to connect to MySQL database to retrieve or add data.
        In my main class, I create the connection (only one time) using this code:

        QSqlDatabase base = QSqlDatabase::addDatabase("QMYSQL");
            base.setHostName("");
            base.setUserName("");
            base.setPassword("");
            base.setDatabaseName("");
            if(!base.open(){
                baseOpened=false;//boolean
            }
            else{        baseOpened=true;}
            base.close();
            base = QSqlDatabase();
        

        After that, when i need to retreive data I use this code

        QSqlDatabase db = QSqlDatabase::database();
            QSqlQuery query;
            if(db.isOpen())
            {
                query.exec("SELECT * ...");
                ...
                db.close();
            }
        

        the problem is that when i let the application open for longtime i get an error "too many open files"

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 18 Oct 2016, 04:21 last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • S SGaist
          17 Oct 2016, 21:46

          Hi,

          Why are you closing the connection each time ?

          M Offline
          M Offline
          madababi
          wrote on 18 Oct 2016, 07:14 last edited by
          #4

          @SGaist
          I have many tables and I access them from differents classes.
          I think that

          QSqlDatabase db = QSqlDatabase::database();
          

          open the database connection created in my main class so I need to close it.
          I don't know if this is the right way to do this so can you tell me if i need to close the connection each time ?

          J 1 Reply Last reply 18 Oct 2016, 08:26
          0
          • M madababi
            18 Oct 2016, 07:14

            @SGaist
            I have many tables and I access them from differents classes.
            I think that

            QSqlDatabase db = QSqlDatabase::database();
            

            open the database connection created in my main class so I need to close it.
            I don't know if this is the right way to do this so can you tell me if i need to close the connection each time ?

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 18 Oct 2016, 08:26 last edited by
            #5

            @madababi If you close the connection how then you want to access the database?
            Keep the connection open and close it if your application is terminating or you know that you don't need it anymore.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 18 Oct 2016, 09:04 last edited by
              #6

              No it doesn't open anything. It returns an object that represent a connection, in this case, the default connection since you don't pass any parameter.

              If you close it somewhere it will stay closed until you re-open it.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              1/6

              17 Oct 2016, 14:41

              • Login

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