Important: Please read the Qt Code of Conduct -

Recommendation for codeing practice

  • 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?

  • Qt Champions 2019

    @bart.hollis said in Recommendation for codeing practice:

    Should I set a global variable to be used for queries

    It is not really clear what exactly you're talking about? DB connection? Your queries?
    DB connections are already handled by QDatabase (
    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.

  • Lifetime Qt Champion

    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
      if (! {
        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) :
      ui(new Ui::MainWindow) {
      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");
      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.

  • Qt Champions 2017

    @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



    (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.

Log in to reply