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 Updated to NodeBB v4.3 + New Features

The correct way to open mysql connection

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.0k Views 1 Watching
  • 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.
  • madababiM Offline
    madababiM Offline
    madababi
    wrote on 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"

    jsulmJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on 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

      madababiM 1 Reply Last reply
      0
      • madababiM madababi

        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"

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Why are you closing the connection each time ?

          madababiM Offline
          madababiM Offline
          madababi
          wrote on 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 ?

          jsulmJ 1 Reply Last reply
          0
          • madababiM madababi

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

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on 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
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on 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

              • Login

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