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. Multiple classes DataBase connection
Forum Updated to NodeBB v4.3 + New Features

Multiple classes DataBase connection

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 1.2k 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.
  • P Offline
    P Offline
    PaulWWW
    wrote on 9 Jun 2019, 09:13 last edited by
    #1

    Hello! I'm new to Qt and I'm writing an application which interacts with database and have multiple QDialogs in it. What is an optimal way to connect all my classes with database? I mean, should I create a global connection class or just connect to database in every class separately from others? If it's better to create a global connection class, how to do that?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 9 Jun 2019, 09:19 last edited by mrjj 6 Sept 2019, 09:21
      #2

      Hi and welcome to the forums.-
      If you mean to access the same database from multiple classes,
      then Qt already can do this for you.

      bool createConnection()
      {
          QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");  << << Looks like a local var but is kept internally
          db.setDatabaseName(":memory:");
          //  db.setDatabaseName("c:\\folder\\test.db");
          if (!db.open()) {
              QMessageBox::critical(0, qApp->tr("Cannot open database"), "Click Cancel to exit.",
                                    QMessageBox::Cancel);
              return false;
          }
          QSqlQuery query;
          qDebug() << "table:" <<   query.exec("create table person (id int primary key, "
                                               "firstname varchar(20), lastname varchar(20), num int )");
          query.exec("insert into person (firstname , lastname, num) values('Dennis', 'Young','1')");
          query.exec("insert into person values(102, 'Christine', 'Holand','2')");
          query.exec("insert into person values(103, 'Lars junior', 'Gordon','4')");
          query.exec("insert into person values(104, 'Roberto', 'Robitaille','5')");
          query.exec("insert into person values(105, 'Maria', 'Papadopoulos','3')");
          return true;
      }
      

      later we can just do

      void ProcessDB()
      {
          QSqlQuery query;
          bool ok = query.prepare("SELECT * from person ");
          query.exec();
          while (query.next()) {
              QString name = query.value(2).toString(); // col 1 = name
              qDebug() << ">" << name;
          }
      }
      

      and QSqlQuery uses the default database we open in the other function.

      so there is no need to transfer the DB instance around.

      However, i can highly recommend hiding all QSqlQuery related stuff in a class so
      the whole program does not have to know how it's stored in the database.

      1 Reply Last reply
      2
      • P Offline
        P Offline
        PaulWWW
        wrote on 9 Jun 2019, 09:27 last edited by PaulWWW 6 Sept 2019, 09:30
        #3

        @mrjj Thanks for such a fast answer!
        So, you mean, I should better create a conenction class, paste there all the connection code like the first part from your post and in other class just use QSqlQuery and it will connect to the same database without any extra code?

        M 1 Reply Last reply 9 Jun 2019, 09:53
        0
        • P PaulWWW
          9 Jun 2019, 09:27

          @mrjj Thanks for such a fast answer!
          So, you mean, I should better create a conenction class, paste there all the connection code like the first part from your post and in other class just use QSqlQuery and it will connect to the same database without any extra code?

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 9 Jun 2019, 09:53 last edited by mrjj 6 Sept 2019, 09:55
          #4

          @PaulWWW
          Hi
          Well you should just open the database somewhere at startup and you can use
          QSqlQuery ( and it will indirectly use that default connection ) you have opened, from
          anywhere else in the project. and Yes, no extra code needed as long as you just use one database for whole
          app. ( a database can contain multiple tables etc so normally no need for multiple database files)

          P 1 Reply Last reply 9 Jun 2019, 10:04
          2
          • M mrjj
            9 Jun 2019, 09:53

            @PaulWWW
            Hi
            Well you should just open the database somewhere at startup and you can use
            QSqlQuery ( and it will indirectly use that default connection ) you have opened, from
            anywhere else in the project. and Yes, no extra code needed as long as you just use one database for whole
            app. ( a database can contain multiple tables etc so normally no need for multiple database files)

            P Offline
            P Offline
            PaulWWW
            wrote on 9 Jun 2019, 10:04 last edited by
            #5

            @mrjj Thank you very much for help!

            M 1 Reply Last reply 9 Jun 2019, 10:20
            0
            • P PaulWWW
              9 Jun 2019, 10:04

              @mrjj Thank you very much for help!

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 9 Jun 2019, 10:20 last edited by
              #6

              @PaulWWW
              Most welcome.
              Please note Qt have models for databases.
              https://doc.qt.io/qt-5/sql-model.html
              So say editing a full table can be done with Qt predefined class and no extra code so make sure
              you look into those to avoid manually updates when not really needed.

              1 Reply Last reply
              2

              1/6

              9 Jun 2019, 09:13

              • 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