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. QSQlQuery::exec: database not open
Forum Updated to NodeBB v4.3 + New Features

QSQlQuery::exec: database not open

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 5 Posters 11.9k Views 3 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.
  • O Offline
    O Offline
    Omar Alkersh
    wrote on 3 Apr 2017, 19:53 last edited by
    #1

    Hello,

    So I have been receiving this error while running the following code

    void MainWindow::on_serachBox_textEdited(const QString& arg1) {
      // TODO: fix this
      // databse::exec: databse not open
      queryModel.setQuery("SELECT * FROM " + ui->comboBox->currentText() +
                              " WHERE " +
                              handler->getMainField(ui->comboBox->currentText()) +
                              " LIKE \'%" + arg1 + "%\'",
                          handler->getDB());
      qDebug() << queryModel.lastError();
      qDebug() << queryModel.query().lastQuery();
      ui->dataOutput->setModel(&queryModel);
    }
    

    This method runs when the text in line edit changed. I tried doing db->open(); before running thequeryModel.setQuery(...); but got no different result.
    What is really frustrating is when I run the following code, it runs without an issue:

    void MainWindow::updateQuery() {
      handler->open();
      queryModel.setQuery("SELECT * FROM " + ui->comboBox->currentText() +
                          " WHERE 1");
      handler->close();
      ui->dataOutput->setModel(&queryModel);
    }
    

    Can someone explain to me why I am getting this error and how I can fix it?

    Thank you.

    Application output:

    QSqlQuery::exec: database not open
    QSqlError("", "", "")
    "SELECT * FROM test1 WHERE testF_T LIKE '%3%'"
    

    PS.
    The query itself is fine, I checked it

    K J 2 Replies Last reply 3 Apr 2017, 20:16
    0
    • O Omar Alkersh
      3 Apr 2017, 19:53

      Hello,

      So I have been receiving this error while running the following code

      void MainWindow::on_serachBox_textEdited(const QString& arg1) {
        // TODO: fix this
        // databse::exec: databse not open
        queryModel.setQuery("SELECT * FROM " + ui->comboBox->currentText() +
                                " WHERE " +
                                handler->getMainField(ui->comboBox->currentText()) +
                                " LIKE \'%" + arg1 + "%\'",
                            handler->getDB());
        qDebug() << queryModel.lastError();
        qDebug() << queryModel.query().lastQuery();
        ui->dataOutput->setModel(&queryModel);
      }
      

      This method runs when the text in line edit changed. I tried doing db->open(); before running thequeryModel.setQuery(...); but got no different result.
      What is really frustrating is when I run the following code, it runs without an issue:

      void MainWindow::updateQuery() {
        handler->open();
        queryModel.setQuery("SELECT * FROM " + ui->comboBox->currentText() +
                            " WHERE 1");
        handler->close();
        ui->dataOutput->setModel(&queryModel);
      }
      

      Can someone explain to me why I am getting this error and how I can fix it?

      Thank you.

      Application output:

      QSqlQuery::exec: database not open
      QSqlError("", "", "")
      "SELECT * FROM test1 WHERE testF_T LIKE '%3%'"
      

      PS.
      The query itself is fine, I checked it

      K Offline
      K Offline
      koahnig
      wrote on 3 Apr 2017, 20:16 last edited by
      #2

      @Omar-Alkersh

      Hi and welcome to devnet forum

      There is not need to have a pointer for the db. You can simply open the default as it is shown in the detailed description here. However, you have to open the db prior to access.

      Vote the answer(s) that helped you to solve your issue(s)

      O 1 Reply Last reply 4 Apr 2017, 08:44
      1
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 3 Apr 2017, 20:39 last edited by
        #3

        Hi and welcome to devnet,

        To add to @koahnig, what is that handler object ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        O 1 Reply Last reply 4 Apr 2017, 08:39
        0
        • S SGaist
          3 Apr 2017, 20:39

          Hi and welcome to devnet,

          To add to @koahnig, what is that handler object ?

          O Offline
          O Offline
          Omar Alkersh
          wrote on 4 Apr 2017, 08:39 last edited by
          #4

          @SGaist just a normal object where I keep all my db related methods and object, QSqlQuery, QSqlDatabse etc.I

          1 Reply Last reply
          0
          • K koahnig
            3 Apr 2017, 20:16

            @Omar-Alkersh

            Hi and welcome to devnet forum

            There is not need to have a pointer for the db. You can simply open the default as it is shown in the detailed description here. However, you have to open the db prior to access.

            O Offline
            O Offline
            Omar Alkersh
            wrote on 4 Apr 2017, 08:44 last edited by
            #5

            @koahnig I open it at the constructor of the handler object, an object where I keep all the db related methods and vars, to keep things clean

            1 Reply Last reply
            0
            • O Omar Alkersh
              3 Apr 2017, 19:53

              Hello,

              So I have been receiving this error while running the following code

              void MainWindow::on_serachBox_textEdited(const QString& arg1) {
                // TODO: fix this
                // databse::exec: databse not open
                queryModel.setQuery("SELECT * FROM " + ui->comboBox->currentText() +
                                        " WHERE " +
                                        handler->getMainField(ui->comboBox->currentText()) +
                                        " LIKE \'%" + arg1 + "%\'",
                                    handler->getDB());
                qDebug() << queryModel.lastError();
                qDebug() << queryModel.query().lastQuery();
                ui->dataOutput->setModel(&queryModel);
              }
              

              This method runs when the text in line edit changed. I tried doing db->open(); before running thequeryModel.setQuery(...); but got no different result.
              What is really frustrating is when I run the following code, it runs without an issue:

              void MainWindow::updateQuery() {
                handler->open();
                queryModel.setQuery("SELECT * FROM " + ui->comboBox->currentText() +
                                    " WHERE 1");
                handler->close();
                ui->dataOutput->setModel(&queryModel);
              }
              

              Can someone explain to me why I am getting this error and how I can fix it?

              Thank you.

              Application output:

              QSqlQuery::exec: database not open
              QSqlError("", "", "")
              "SELECT * FROM test1 WHERE testF_T LIKE '%3%'"
              

              PS.
              The query itself is fine, I checked it

              J Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 4 Apr 2017, 08:58 last edited by
              #6

              @Omar-Alkersh

              the only difference between the 2 code examples you posted is, that in one you pass the database to the queryModel.

              You said you tested it, and it did not work, what exactly is the result when you would change the function to this:

              void MainWindow::on_serachBox_textEdited(const QString& arg1) {
              
              handler->open();
                queryModel.setQuery("SELECT * FROM " + ui->comboBox->currentText() +
                                        " WHERE " +
                                        handler->getMainField(ui->comboBox->currentText()) +
                                        " LIKE \'%" + arg1 + "%\'");
                qDebug() << queryModel.lastError();
                qDebug() << queryModel.query().lastQuery();
                handler->close();
                ui->dataOutput->setModel(&queryModel);
              }
              

              I haven't used the QSqlQueryModel class myself yet, but are you supposed to to piece the query together like this!? Seems wrong.


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              O 1 Reply Last reply 4 Apr 2017, 10:21
              0
              • J J.Hilk
                4 Apr 2017, 08:58

                @Omar-Alkersh

                the only difference between the 2 code examples you posted is, that in one you pass the database to the queryModel.

                You said you tested it, and it did not work, what exactly is the result when you would change the function to this:

                void MainWindow::on_serachBox_textEdited(const QString& arg1) {
                
                handler->open();
                  queryModel.setQuery("SELECT * FROM " + ui->comboBox->currentText() +
                                          " WHERE " +
                                          handler->getMainField(ui->comboBox->currentText()) +
                                          " LIKE \'%" + arg1 + "%\'");
                  qDebug() << queryModel.lastError();
                  qDebug() << queryModel.query().lastQuery();
                  handler->close();
                  ui->dataOutput->setModel(&queryModel);
                }
                

                I haven't used the QSqlQueryModel class myself yet, but are you supposed to to piece the query together like this!? Seems wrong.

                O Offline
                O Offline
                Omar Alkersh
                wrote on 4 Apr 2017, 10:21 last edited by Omar Alkersh 4 Apr 2017, 10:21
                #7

                @J.Hilk hello,
                The result is that I get the same meessge, QSqlQuery::exec: database not open, regardless whether I write handler->ope (); or not.
                The QTableView, which I use this model with, would stop displaying any table.

                Basically no difference.

                jsulmJ 1 Reply Last reply 4 Apr 2017, 10:45
                0
                • O Omar Alkersh
                  4 Apr 2017, 10:21

                  @J.Hilk hello,
                  The result is that I get the same meessge, QSqlQuery::exec: database not open, regardless whether I write handler->ope (); or not.
                  The QTableView, which I use this model with, would stop displaying any table.

                  Basically no difference.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 4 Apr 2017, 10:45 last edited by
                  #8

                  @Omar-Alkersh Can you show the content of handler->open() ?

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

                  O 1 Reply Last reply 4 Apr 2017, 11:01
                  0
                  • jsulmJ jsulm
                    4 Apr 2017, 10:45

                    @Omar-Alkersh Can you show the content of handler->open() ?

                    O Offline
                    O Offline
                    Omar Alkersh
                    wrote on 4 Apr 2017, 11:01 last edited by
                    #9

                    @jsulm

                    
                    QSqlDatabase db; //initialised in constructor
                    
                    void BDHandler::open (){
                    db->open ();
                    }
                    
                    handler->close (); //same as open
                    
                    jsulmJ 1 Reply Last reply 4 Apr 2017, 11:17
                    0
                    • O Omar Alkersh
                      4 Apr 2017, 11:01

                      @jsulm

                      
                      QSqlDatabase db; //initialised in constructor
                      
                      void BDHandler::open (){
                      db->open ();
                      }
                      
                      handler->close (); //same as open
                      
                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 4 Apr 2017, 11:17 last edited by
                      #10

                      @Omar-Alkersh said in QSQlQuery::exec: database not open:

                      QSqlDatabase db;

                      If this is like this in the constructor then it is a local variable in the constructor and not related to the db pointer!
                      You also should check the return value of db->open () and print out the output of http://doc.qt.io/qt-4.8/qsqldatabase.html#lastError in case it returns false. Also you did not say how you set up your database.

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

                      O 1 Reply Last reply 4 Apr 2017, 17:22
                      0
                      • jsulmJ jsulm
                        4 Apr 2017, 11:17

                        @Omar-Alkersh said in QSQlQuery::exec: database not open:

                        QSqlDatabase db;

                        If this is like this in the constructor then it is a local variable in the constructor and not related to the db pointer!
                        You also should check the return value of db->open () and print out the output of http://doc.qt.io/qt-4.8/qsqldatabase.html#lastError in case it returns false. Also you did not say how you set up your database.

                        O Offline
                        O Offline
                        Omar Alkersh
                        wrote on 4 Apr 2017, 17:22 last edited by
                        #11

                        @jsulm

                        Thanks for the suggestion but this is a global variable, it is just initialized in the constructor.

                        And this is how I set up my db

                        DBHandler::DBHandler(MainWindow* mWindow) {
                          // init vars
                          path = databaseloc.currentPath() + "/database";
                          TABLE_MAIN = "main_table";
                          COLUMN_ID = "_id";
                          COLUMN_MAIN_NAME = "main_name";
                        
                          // check if dir exits
                          QFileInfo fileInfo(path);
                          if (!fileInfo.exists()) {
                            if (databaseloc.mkpath(path)) {
                              mWindow->close();
                            }
                          }
                        
                          // db name and loc
                          path += "/" + dbName + ".db";
                        
                          // add db
                          db = QSqlDatabase::addDatabase("QSQLITE");
                          db.setDatabaseName(path);
                        
                          // open test, sets up query and creates mainTable if not exists
                          if (db.open()) {
                            query = QSqlQuery(db);
                            query.exec("CREATE TABLE IF NOT EXISTS " + TABLE_MAIN + "(" + COLUMN_ID +
                                       " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_MAIN_NAME +
                                       " TEXT, mainField TEXT);");
                        
                          } else {
                            // stop programme from continueing
                            QMessageBox::StandardButton reply;
                        
                            reply = QMessageBox::warning(
                                mWindow, "Error",
                                "Was not able to open database file.\n" + db.lastError().text());
                        
                            if (reply == QMessageBox::Ok) {
                              // exits with error
                              exit(1);
                            }
                          }
                          db.close();
                        }
                        
                        
                        K 1 Reply Last reply 4 Apr 2017, 18:08
                        0
                        • O Omar Alkersh
                          4 Apr 2017, 17:22

                          @jsulm

                          Thanks for the suggestion but this is a global variable, it is just initialized in the constructor.

                          And this is how I set up my db

                          DBHandler::DBHandler(MainWindow* mWindow) {
                            // init vars
                            path = databaseloc.currentPath() + "/database";
                            TABLE_MAIN = "main_table";
                            COLUMN_ID = "_id";
                            COLUMN_MAIN_NAME = "main_name";
                          
                            // check if dir exits
                            QFileInfo fileInfo(path);
                            if (!fileInfo.exists()) {
                              if (databaseloc.mkpath(path)) {
                                mWindow->close();
                              }
                            }
                          
                            // db name and loc
                            path += "/" + dbName + ".db";
                          
                            // add db
                            db = QSqlDatabase::addDatabase("QSQLITE");
                            db.setDatabaseName(path);
                          
                            // open test, sets up query and creates mainTable if not exists
                            if (db.open()) {
                              query = QSqlQuery(db);
                              query.exec("CREATE TABLE IF NOT EXISTS " + TABLE_MAIN + "(" + COLUMN_ID +
                                         " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_MAIN_NAME +
                                         " TEXT, mainField TEXT);");
                          
                            } else {
                              // stop programme from continueing
                              QMessageBox::StandardButton reply;
                          
                              reply = QMessageBox::warning(
                                  mWindow, "Error",
                                  "Was not able to open database file.\n" + db.lastError().text());
                          
                              if (reply == QMessageBox::Ok) {
                                // exits with error
                                exit(1);
                              }
                            }
                            db.close();
                          }
                          
                          
                          K Offline
                          K Offline
                          koahnig
                          wrote on 4 Apr 2017, 18:08 last edited by
                          #12

                          @Omar-Alkersh

                          Did you notice the close at the end of your constructor?

                           }
                            db.close();
                          }
                          

                          I guess there is your problem.

                          Vote the answer(s) that helped you to solve your issue(s)

                          1 Reply Last reply
                          3
                          • S Offline
                            S Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 4 Apr 2017, 19:30 last edited by
                            #13

                            Hi,

                            To add to @koahnig, why are you opening and closing the database connection again and again ?

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            O 1 Reply Last reply 5 Apr 2017, 07:16
                            0
                            • S SGaist
                              4 Apr 2017, 19:30

                              Hi,

                              To add to @koahnig, why are you opening and closing the database connection again and again ?

                              O Offline
                              O Offline
                              Omar Alkersh
                              wrote on 5 Apr 2017, 07:16 last edited by Omar Alkersh 4 May 2017, 09:48
                              #14

                              @SGaist Thank you good sir. It worked. I thought that I should always close the db to stop any memory leaks. I used to develop android and I used to always close it after me.

                              Solution was not to close the db. Obviously.

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on 5 Apr 2017, 21:05 last edited by
                                #15

                                AFAIK, there's no known memory leaks in Qt's SQL drivers.

                                Don't get me wrong, there might be good reasons to only open a database connection when needed however this requires a bit more architecture to make it work correctly.

                                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

                                1/15

                                3 Apr 2017, 19:53

                                • Login

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