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. Improve efficiency of database connection
Forum Updated to NodeBB v4.3 + New Features

Improve efficiency of database connection

Scheduled Pinned Locked Moved Solved General and Desktop
qsqldatabaseqsqlquery
10 Posts 2 Posters 2.9k 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.
  • R Offline
    R Offline
    reezeus
    wrote on last edited by SGaist
    #1

    Hi I have a class called "SqlConnect" that allows me to connect to an MS SQL Server. The class works well, however this class is called from dozens of other classes, and hence I need to instanciate a new object of the SqlConnect class in each of those classes.

    Here is how the SqlConnect class looks like:
    //Constructor:
    @
    SqlConnect::SqlConnect(QString dbName, QString alias){
    database = QSqlDatabase::addDatabase("QODBC", alias);
    database.setDatabaseName("Driver={SQL Server};Server=myServer;Database=" + dbName + ";");
    database.setUserName("myUsername");
    database.setPassword("myPassword");

    database.open();
    

    }
    @

    //Main function (to request the DB):
    @
    QSqlQuery SqlConnect::GetResultsFromQuery(QString command){
    query = QSqlQuery(database);
    query.setForwardOnly(true);
    query.prepare(command);
    query.exec();
    return query;
    }
    @

    Each class using the SqlConnect object is as follow:
    .h:
    @SqlConnect *sql;@
    .cpp:
    @sql = new SqlConnect("databaseName", "callingClassName");@

    Since I'm connecting to only 2 different database in the whole program, I believe I could do much more efficient by having only two instances that can be used at a global scope by all the classes. I have never used global variables since it's known to be a bad practice. Could you please help me and tell me how can I use only one instance (across all my classes) of SqlConnect for each database I want to connect to.

    Thanks much

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

      Hi,

      At startup create your two connections with different names and when you want to execute a query on a given database just use:

      QSqlDatabase db = QSqlDatabase::database("my connection name");
      QSqlQuery query(db);
      ...//rest of your code
      

      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
      • R Offline
        R Offline
        reezeus
        wrote on last edited by
        #3

        Hi SGaist

        Thanks for your help (again).
        What do you mean at startup? In which class?

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

          Your application startup, it can be done in the main function or in a class that you see fit for that.

          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
          • R Offline
            R Offline
            reezeus
            wrote on last edited by
            #5

            Let's say I put it in the main function (which is alone in the main class in my case), then 'db' will exists only in the main function ? I have others useful function in SqlConnect like bulk insert, etc. so I really would like to keep using it..

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

              The db variable yes but the related connection itself no. You can retrieve it where you want using the method I mentioned above.

              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
              • R Offline
                R Offline
                reezeus
                wrote on last edited by
                #7

                Oh! My mistake I read QSqlDatabase::addDatabase instead of QSqlDatabase::database. Ok I'm trying it now.
                Thanks.

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  reezeus
                  wrote on last edited by
                  #8

                  Works like a charm. Thanks much for your help.

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

                    You're welcome !

                    No need to modify the title anymore, just use the "Topic Tool" button to mark the thread as solved :)

                    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
                    • R Offline
                      R Offline
                      reezeus
                      wrote on last edited by
                      #10

                      Done :)

                      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