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. How to correctly use database
Servers for Qt installer are currently down

How to correctly use database

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 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.
  • T Offline
    T Offline
    Thank You
    wrote on 10 Aug 2021, 06:17 last edited by Thank You 8 Oct 2021, 06:38
    #1

    I have asked many questions related to database where I got many important information.
    Now I want to get your thoughts on how to manage database connections properly for better speed security. I will list my questions below:

    • How many connections should I create in application(application with atleast 10-20 pages) which means 60 different connections(or 60 diff queries). I guess @JonB had said about it a bit. But
      • Should I create one main connection and use it for all queries?
      • It consists of 10 tables(tableview or model NOT of database) for which now it consists different connections
    • I am currently opening database in constructor and removing in destructor. Is it good way to do this?
    myClass::myClass(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::myClass)
    {
        ui->setupUi(this);
        filledTable = QSqlDatabase::addDatabase("QMYSQL","filled-table");
    // filledTable is created as private member of this class
    
        filledTable.setDatabaseName(databaseName);
        filledTable.setPort(databasePort);
        filledTable.setHostName(databaseHostname);
        filledTable.setUserName(databaseUsername);
        filledTable.setPassword(databasePassword);
    
    }
    
    myClass::~myClass()
    {
        QSqlDatabase::removeDatabase("filled-table");
        delete ui;
    }
    

    And supose if I have to connect database for any other purpose I am using different connection. But In my thought I should create two connections for page One for filling tables and another for filling all datas except tables

    https://forum.qt.io/topic/127056/using-model-to-fill-tableview-with-qmysql/
    This is one which I had solved using the same concept

    Or Should I create a single connection for whole application and use it for every task

    Btw, I am using MYSQL as database

    Let's make QT free or It will go forever

    TRUE AND FALSE <3

    J 1 Reply Last reply 10 Aug 2021, 06:23
    0
    • T Thank You
      13 Aug 2021, 07:14

      @jsulm

      // Later somewhere else use the connection:
      QSqlDatabase db = QSqlDatabase::database();

      Wow <3
      It is absolutely new concept for me. I haven't seen this before , Amazing
      Some simple question,
      Should I close the connection in destructor?
      What if I have two widgets

      • first
      • second
        And I have the connection in first , Can I use the same connection in second widget If both of them are two non related separate files

      Then I should never put database connection in private/public variables. just create connection in constructor and use it as you have mentioned in answer . Just close the connection in destructor.
      And one connection is perfect for one database, There are no need of different connection on different widgets

      J Online
      J Online
      jsulm
      Lifetime Qt Champion
      wrote on 13 Aug 2021, 07:32 last edited by
      #6

      @Thank-You said in How to correctly use database:

      Should I close the connection in destructor?

      Close it when you don't need it anymore. Destructor is one place where you can do it.

      "And I have the connection in first" - you do not have it in first. A connection is dedicated to one thread and you can use it everywhere in that thread (in your case in UI thread). So, you can use it in first, in second and whereelse in your UI. You can call QSqlDatabase::database() everywhere in your code to get the connection.

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

      T 1 Reply Last reply 13 Aug 2021, 09:45
      0
      • T Thank You
        10 Aug 2021, 06:17

        I have asked many questions related to database where I got many important information.
        Now I want to get your thoughts on how to manage database connections properly for better speed security. I will list my questions below:

        • How many connections should I create in application(application with atleast 10-20 pages) which means 60 different connections(or 60 diff queries). I guess @JonB had said about it a bit. But
          • Should I create one main connection and use it for all queries?
          • It consists of 10 tables(tableview or model NOT of database) for which now it consists different connections
        • I am currently opening database in constructor and removing in destructor. Is it good way to do this?
        myClass::myClass(QWidget *parent) :
            QWidget(parent),
            ui(new Ui::myClass)
        {
            ui->setupUi(this);
            filledTable = QSqlDatabase::addDatabase("QMYSQL","filled-table");
        // filledTable is created as private member of this class
        
            filledTable.setDatabaseName(databaseName);
            filledTable.setPort(databasePort);
            filledTable.setHostName(databaseHostname);
            filledTable.setUserName(databaseUsername);
            filledTable.setPassword(databasePassword);
        
        }
        
        myClass::~myClass()
        {
            QSqlDatabase::removeDatabase("filled-table");
            delete ui;
        }
        

        And supose if I have to connect database for any other purpose I am using different connection. But In my thought I should create two connections for page One for filling tables and another for filling all datas except tables

        https://forum.qt.io/topic/127056/using-model-to-fill-tableview-with-qmysql/
        This is one which I had solved using the same concept

        Or Should I create a single connection for whole application and use it for every task

        Btw, I am using MYSQL as database

        J Online
        J Online
        jsulm
        Lifetime Qt Champion
        wrote on 10 Aug 2021, 06:23 last edited by
        #2

        @Thank-You said in How to correctly use database:

        which means 60 different process

        What do you mean by "process" here? One running application is one process, doesn't matter how many "pages" (whatever you mean by that) it has. Do you maybe mean threads instead of processes?
        There is absolutely no need to have one connection per table. A connection is for one database, doesn't matter how many table the database has.
        You also should not store the database connection in variables, this is explained in documentation:
        "Warning: It is highly recommended that you do not keep a copy of the QSqlDatabase around as a member of a class, as this will prevent the instance from being correctly cleaned up on shutdown. If you need to access an existing QSqlDatabase, it should be accessed with database(). If you chose to have a QSqlDatabase member variable, this needs to be deleted before the QCoreApplication instance is deleted, otherwise it may lead to undefined behavior."
        I'm reffering to this:

        filledTable = QSqlDatabase::addDatabase("QMYSQL","filled-table");
        

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

        T 1 Reply Last reply 10 Aug 2021, 06:35
        1
        • J jsulm
          10 Aug 2021, 06:23

          @Thank-You said in How to correctly use database:

          which means 60 different process

          What do you mean by "process" here? One running application is one process, doesn't matter how many "pages" (whatever you mean by that) it has. Do you maybe mean threads instead of processes?
          There is absolutely no need to have one connection per table. A connection is for one database, doesn't matter how many table the database has.
          You also should not store the database connection in variables, this is explained in documentation:
          "Warning: It is highly recommended that you do not keep a copy of the QSqlDatabase around as a member of a class, as this will prevent the instance from being correctly cleaned up on shutdown. If you need to access an existing QSqlDatabase, it should be accessed with database(). If you chose to have a QSqlDatabase member variable, this needs to be deleted before the QCoreApplication instance is deleted, otherwise it may lead to undefined behavior."
          I'm reffering to this:

          filledTable = QSqlDatabase::addDatabase("QMYSQL","filled-table");
          
          T Offline
          T Offline
          Thank You
          wrote on 10 Aug 2021, 06:35 last edited by Thank You 8 Oct 2021, 06:37
          #3

          @jsulm
          Yes Sir, I have corrected it

          What do you mean by "process" here?

          Maybe query would make sense. Sorry, I don't mean threads

          There is absolutely no need to have one connection per table.
          I mean tableview not database table, Sorry to have vague question.
          You also should not store the database connection in variables, this is explained in documentation:
          No these variables are used as private variable , So I can't access from others.
          If that is the problem , What techniques should I use it

          https://forum.qt.io/topic/127056/using-model-to-fill-tableview-with-qmysql/21
          I got some concepts from this post points

          • Don't remove the database while you have a view/model using it.
          • Don't re-add the same database while you have previously added it and not yet removed it.

          Let's make QT free or It will go forever

          TRUE AND FALSE <3

          J 1 Reply Last reply 10 Aug 2021, 08:23
          0
          • T Thank You
            10 Aug 2021, 06:35

            @jsulm
            Yes Sir, I have corrected it

            What do you mean by "process" here?

            Maybe query would make sense. Sorry, I don't mean threads

            There is absolutely no need to have one connection per table.
            I mean tableview not database table, Sorry to have vague question.
            You also should not store the database connection in variables, this is explained in documentation:
            No these variables are used as private variable , So I can't access from others.
            If that is the problem , What techniques should I use it

            https://forum.qt.io/topic/127056/using-model-to-fill-tableview-with-qmysql/21
            I got some concepts from this post points

            • Don't remove the database while you have a view/model using it.
            • Don't re-add the same database while you have previously added it and not yet removed it.
            J Online
            J Online
            jsulm
            Lifetime Qt Champion
            wrote on 10 Aug 2021, 08:23 last edited by
            #4

            @Thank-You said in How to correctly use database:

            No these variables are used as private variable , So I can't access from others.
            If that is the problem , What techniques should I use it

            Why do you think you need these variables? How to use a database connection is explained and shown in documentation (https://doc.qt.io/qt-5/qsqldatabase.html):

            // Initialize connection and open:
            QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
            db.setHostName("acidalia");
            db.setDatabaseName("customdb");
            db.setUserName("mojito");
            db.setPassword("J0a1m8");
            bool ok = db.open();
            
            // Later somewhere else use the connection:
            QSqlDatabase db = QSqlDatabase::database();
            

            So, you simply call QSqlDatabase::database() to get the connection by name you want to use. There is really no need for any variables, private or not.

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

            T 1 Reply Last reply 13 Aug 2021, 07:14
            3
            • J jsulm
              10 Aug 2021, 08:23

              @Thank-You said in How to correctly use database:

              No these variables are used as private variable , So I can't access from others.
              If that is the problem , What techniques should I use it

              Why do you think you need these variables? How to use a database connection is explained and shown in documentation (https://doc.qt.io/qt-5/qsqldatabase.html):

              // Initialize connection and open:
              QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
              db.setHostName("acidalia");
              db.setDatabaseName("customdb");
              db.setUserName("mojito");
              db.setPassword("J0a1m8");
              bool ok = db.open();
              
              // Later somewhere else use the connection:
              QSqlDatabase db = QSqlDatabase::database();
              

              So, you simply call QSqlDatabase::database() to get the connection by name you want to use. There is really no need for any variables, private or not.

              T Offline
              T Offline
              Thank You
              wrote on 13 Aug 2021, 07:14 last edited by
              #5

              @jsulm

              // Later somewhere else use the connection:
              QSqlDatabase db = QSqlDatabase::database();

              Wow <3
              It is absolutely new concept for me. I haven't seen this before , Amazing
              Some simple question,
              Should I close the connection in destructor?
              What if I have two widgets

              • first
              • second
                And I have the connection in first , Can I use the same connection in second widget If both of them are two non related separate files

              Then I should never put database connection in private/public variables. just create connection in constructor and use it as you have mentioned in answer . Just close the connection in destructor.
              And one connection is perfect for one database, There are no need of different connection on different widgets

              Let's make QT free or It will go forever

              TRUE AND FALSE <3

              J 1 Reply Last reply 13 Aug 2021, 07:32
              0
              • T Thank You
                13 Aug 2021, 07:14

                @jsulm

                // Later somewhere else use the connection:
                QSqlDatabase db = QSqlDatabase::database();

                Wow <3
                It is absolutely new concept for me. I haven't seen this before , Amazing
                Some simple question,
                Should I close the connection in destructor?
                What if I have two widgets

                • first
                • second
                  And I have the connection in first , Can I use the same connection in second widget If both of them are two non related separate files

                Then I should never put database connection in private/public variables. just create connection in constructor and use it as you have mentioned in answer . Just close the connection in destructor.
                And one connection is perfect for one database, There are no need of different connection on different widgets

                J Online
                J Online
                jsulm
                Lifetime Qt Champion
                wrote on 13 Aug 2021, 07:32 last edited by
                #6

                @Thank-You said in How to correctly use database:

                Should I close the connection in destructor?

                Close it when you don't need it anymore. Destructor is one place where you can do it.

                "And I have the connection in first" - you do not have it in first. A connection is dedicated to one thread and you can use it everywhere in that thread (in your case in UI thread). So, you can use it in first, in second and whereelse in your UI. You can call QSqlDatabase::database() everywhere in your code to get the connection.

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

                T 1 Reply Last reply 13 Aug 2021, 09:45
                0
                • J jsulm
                  13 Aug 2021, 07:32

                  @Thank-You said in How to correctly use database:

                  Should I close the connection in destructor?

                  Close it when you don't need it anymore. Destructor is one place where you can do it.

                  "And I have the connection in first" - you do not have it in first. A connection is dedicated to one thread and you can use it everywhere in that thread (in your case in UI thread). So, you can use it in first, in second and whereelse in your UI. You can call QSqlDatabase::database() everywhere in your code to get the connection.

                  T Offline
                  T Offline
                  Thank You
                  wrote on 13 Aug 2021, 09:45 last edited by
                  #7

                  @jsulm

                  Thanks it helped a lot

                  Let's make QT free or It will go forever

                  TRUE AND FALSE <3

                  1 Reply Last reply
                  1

                  1/7

                  10 Aug 2021, 06:17

                  • Login

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