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. is that the best way to have a connection to database in the software

is that the best way to have a connection to database in the software

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 1.3k Views 2 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.
  • M Offline
    M Offline
    MostafaEzzat
    wrote on last edited by
    #1

    Hi , I've this function in a class called MySqlDatabase

    bool MySqlDatabase::ConnectDB(){
    
    
    
        MySqlDB  = QSqlDatabase::database();
        MySqlDB =   QSqlDatabase::addDatabase("QMYSQL", "FirstConnection");
        MySqlDB.setHostName("localhost") ;
        MySqlDB.setUserName("root");
        MySqlDB.setPassword("mostafa");
        MySqlDB.setDatabaseName("Clinic_Database");
    
       MySqlDB.open() ;
       if(MySqlDB.open())
       {
           QMessageBox::about(0,"Result","db Opned");
           return true ;
    
       }
       else
        {
           qDebug() << "Error in opneing " << MySqlDB.lastError().text();
           return false;
       }
    

    and when i want to use this class to connect to db i include it then use the object like that
    .h of the class that i wanna use this connction in it

    MySqlDatabase connect  ; 
    

    .cpp

    
     connect.ConnectDB();
              QSqlQuery * qry=new QSqlQuery(connect.MySqlDB);
    

    so is that an efficient way to use it ? or make the bool function of connection in main.cpp file ? .. Thanks in advance

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      First thing: don't call open on your database twice.
      Then, do you have several different connection to handle ? If not, then there's no need for "FirstConnection". A default connection will be created and can be used through out your application.

      Last but not least, as shown and explain in the QSqlDatabase documentation, don't keep QSqlDatabase objects around. When you need one from a specific connection use QSqlDatabase::database.

      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
      2
      • SGaistS SGaist

        Hi,

        First thing: don't call open on your database twice.
        Then, do you have several different connection to handle ? If not, then there's no need for "FirstConnection". A default connection will be created and can be used through out your application.

        Last but not least, as shown and explain in the QSqlDatabase documentation, don't keep QSqlDatabase objects around. When you need one from a specific connection use QSqlDatabase::database.

        M Offline
        M Offline
        MostafaEzzat
        wrote on last edited by
        #3

        @SGaist said in is that the best way to have a connection to database in the software:

        Hi,

        First thing: don't call open on your database twice.
        Then, do you have several different connection to handle ? If not, then there's no need for "FirstConnection". A default connection will be created and can be used through out your application.

        Last but not least, as shown and explain in the QSqlDatabase documentation, don't keep QSqlDatabase objects around. When you need one from a specific connection use QSqlDatabase::database.

        so should i delete this class and rewrite the function of connection in the classes that need a connection to database to prevent QSqlDatabase objects around
        for example : i've 5 classes and a lot of functions so should i write the connection in every class and use the function of connection or make one single function in a class and include this class and use his object ..

        and pls which cases should i use MultiConnection of database e.g .. "FirstConnction" , "SecConnection"

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Nothing wrong with a database manager class.

          Which cases ? If you want to do multithreading access to the database.

          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
          0
          • SGaistS SGaist

            Nothing wrong with a database manager class.

            Which cases ? If you want to do multithreading access to the database.

            M Offline
            M Offline
            MostafaEzzat
            wrote on last edited by MostafaEzzat
            #5

            @SGaist
            but you saying it's not popular to make QSqlDatabase objects around .. so what should i do in this case coz i'll include MySqlDatabase class in 5 classes and ofcourse i'll use the objects in every function

            and if i used multithreading access to database .. will queries and connections handle at Once or they will be queued ?

            Christian EhrlicherC 1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Yes I did, however there's nothing wrong with having class to help setup the various connection.

              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
              0
              • SGaistS SGaist

                Yes I did, however there's nothing wrong with having class to help setup the various connection.

                M Offline
                M Offline
                MostafaEzzat
                wrote on last edited by
                #7

                @SGaist
                Thank you , but pls i've last inquiry about MemoryLeaks .. should i destroy every thing i use in the class in the Heap .. when the class it self calling the destructor e.g.

                "ClassName"::~ClassName()
                

                or he will destroy every thing automaticly and is there any tools help me to discover MemoryLeaks in QT or seeing what's going on in the Heap

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  It depends on what you have as member variables your class.

                  You can use tools like Valgrind to check for memory leaks.

                  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
                  1
                  • M MostafaEzzat

                    @SGaist
                    but you saying it's not popular to make QSqlDatabase objects around .. so what should i do in this case coz i'll include MySqlDatabase class in 5 classes and ofcourse i'll use the objects in every function

                    and if i used multithreading access to database .. will queries and connections handle at Once or they will be queued ?

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @MostafaEzzat said in is that the best way to have a connection to database in the software:

                    and if i used multithreading access to database .. will queries and connections handle at Once or they will be queued ?

                    You need to open a separate DB connection for every thread - see https://doc.qt.io/qt-5/threads-modules.html#threads-and-the-sql-module

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    2

                    • Login

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