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. Recommendation for codeing practice
Forum Updated to NodeBB v4.3 + New Features

Recommendation for codeing practice

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 891 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.
  • bart.hollisB Offline
    bart.hollisB Offline
    bart.hollis
    wrote on last edited by
    #1

    The program I am writing is using SqlLite. The database will have five tables and will be generating quite a few access to the tables. I don't expect it to be used by more than one user at a time, that's why SqlLite. There will be relatively regular access to queries.
    I've watched several youtube tutorials and read all I can but seem to see several differing way to provide this access. This seems to be THE definitive place to get the right answers, so, my question is:
    Should I set a global variable to be used for queries, create a class and pass the handle back, create one class and use a static variable within it, or what?

    jsulmJ 1 Reply Last reply
    0
    • bart.hollisB bart.hollis

      The program I am writing is using SqlLite. The database will have five tables and will be generating quite a few access to the tables. I don't expect it to be used by more than one user at a time, that's why SqlLite. There will be relatively regular access to queries.
      I've watched several youtube tutorials and read all I can but seem to see several differing way to provide this access. This seems to be THE definitive place to get the right answers, so, my question is:
      Should I set a global variable to be used for queries, create a class and pass the handle back, create one class and use a static variable within it, or what?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @bart.hollis said in Recommendation for codeing practice:

      Should I set a global variable to be used for queries

      NO!
      It is not really clear what exactly you're talking about? DB connection? Your queries?
      DB connections are already handled by QDatabase (http://doc.qt.io/qt-5/qsqldatabase.html).
      You could have a manager class for accessing database where you provide a public API hiding the actual access to the database. Only this manager class would contain all the queries.

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

      kshegunovK 1 Reply Last reply
      5
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        Hi
        There is no need to make the Database (variable) global as it can already to that.

        I like what @jsulm suggest - making a class that handles the db and rest of the app will have no idea
        that a database is used. That give a nice modular design and if needed later on, you can use
        a multi user DBMS if needed. So its important to hide the details behind an interface.

        Anyway, small sample to demo that you don't need to keep the
        QSqlDatabase db; around as one easy can think. ( i know i did)

        bool createConnection() {
          QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); // just  a local variable
          db.setDatabaseName(":memory:");
          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 values(101, '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;
        }
        
        
        MainWindow::MainWindow(QWidget* parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow) {
          ui->setupUi(this);
        
          createConnection(); // open db etc
        
          QSqlQuery query; // look ma, no DB variable needed
          int ok = query.prepare(("INSERT INTO person (id, firstname, lastname) VALUES (:name, :first, :last)"));
          query.bindValue(":id", 4);
          query.bindValue(":first", "Lars junior");
          query.bindValue(":last", "Gordon");
          query.exec();
        
          qDebug() << query.lastError() ;
          qDebug() << query.executedQuery();
        
        
        

        Regarding Global variables.
        Besides its considered not good design, with Qt its also not really working.
        Most Qt class do not like to be constructed before QApplication is created.
        Since that happens in Main, its often an issue.

        1 Reply Last reply
        6
        • jsulmJ jsulm

          @bart.hollis said in Recommendation for codeing practice:

          Should I set a global variable to be used for queries

          NO!
          It is not really clear what exactly you're talking about? DB connection? Your queries?
          DB connections are already handled by QDatabase (http://doc.qt.io/qt-5/qsqldatabase.html).
          You could have a manager class for accessing database where you provide a public API hiding the actual access to the database. Only this manager class would contain all the queries.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @jsulm said in Recommendation for codeing practice:

          @bart.hollis said in Recommendation for codeing practice:

          Should I set a global variable to be used for queries

          NO!

          NO!

          (big fat bold letters)

          + @mrjj's nice demo shows why it's not really needed, as QSqlDatabase already provides you with the set of opened connections.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          3

          • Login

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