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. db is not open when called the second time

db is not open when called the second time

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 4 Posters 5.1k 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.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    Hi,
    I have the following function:

    void MainWindow::Addview()
    {
        connectionNames = db.connectionNames ();
    
        if((connectionNames.contains ("qt_sql_default_connection")) == false)
            {
                db = QSqlDatabase::addDatabase ("QSQLITE");
                db.setDatabaseName (fileQstring);
                qDebug() << "Connection Display created in connection(). ";
            }
    
        qDebug() << "Open connection names in Addview: " << db.connectionNames ();
    
    	db.open ();
    
        if(!db.open ())
            {
                qDebug() << "The database (db) is NOT open!";
            }
    
        QSqlQuery query_main  ("SELECT ID, Name,Pic, Description FROM Items ORDER BY NAME ASC ");
    
        if(query_main.isActive()==true)
            {
                qDebug() << "The query is active.";
            }
        else
            {
                qDebug() << "The query is NOT active." << query_main.lastError ();
            }
    
        QStandardItemModel *smodel = new QStandardItemModel(this);
        ui->tableView->setModel (smodel);
    
        for(int row1 = 0; query_main.next (); row1++)
            {
                if(row1 == 0)
                    {
                        const QSqlRecord qRec = query_main.record();
                        qDebug() << "The number of records: " << qRec;
                        smodel->insertColumns (0, qRec.count());
                    }
                smodel->insertRow (row1);
                smodel->setData (smodel->index (row1,0),query_main.value (0).toString ());
                smodel->setData (smodel->index (row1,1),query_main.value (1).toString ());
                Pixmap.loadFromData (query_main.value (2).toByteArray ());
                Pixmap = Pixmap.scaled (100,100,Qt::KeepAspectRatio);
                smodel->setData (smodel->index (row1,2),Pixmap,Qt::DecorationRole);
                smodel->setData (smodel->index (row1,3), query_main.value (3).toString ());
                ui->tableView->setRowHeight (row1,100);
            }
        ui->tableView->horizontalHeader ()->setStyleSheet ("QHeaderView{font: 14pt Arial; color: blue; font-weight: bold;}");
        ui->tableView->verticalHeader ()->setVisible (false);
        ui->tableView->setAlternatingRowColors (true);
        ui->tableView->setStyleSheet ("alternate-background-color: rgb(224,255,248); background-color: white; font: 14pt Arial");
        ui->tableView->setEditTriggers (QAbstractItemView::NoEditTriggers);
        smodel->setHeaderData (0,Qt::Horizontal, QObject::tr ("ID"));
        smodel->setHeaderData (1,Qt::Horizontal, QObject::tr ("Name"));
        smodel->setHeaderData (2,Qt::Horizontal, QObject::tr ("Picture"));
        smodel->setHeaderData (3,Qt::Horizontal, QObject::tr ("Description"));
        ui->tableView->setColumnWidth (1,1);
        ui->tableView->setColumnWidth (1,200);
        ui->tableView->setColumnWidth (2,90);
        ui->tableView->setColumnWidth (3,340);
        ui->tableView->setWordWrap (true);
    }
    

    When it is called the first time (when the program starts) it works fine. When it is called the second time from an other function I get the "The database (db) is NOT open!" error. Why is it not open the second time? Thank you.

    1 Reply Last reply
    0
    • m.sueM Offline
      m.sueM Offline
      m.sue
      wrote on last edited by
      #2

      Hi,
      The second time you call the function, you also open "db" for the second time (without closing it before(?)). The error message may just lead you in the wrong direction.
      -Michael.

      G 1 Reply Last reply
      1
      • m.sueM m.sue

        Hi,
        The second time you call the function, you also open "db" for the second time (without closing it before(?)). The error message may just lead you in the wrong direction.
        -Michael.

        G Offline
        G Offline
        gabor53
        wrote on last edited by
        #3

        @m.sue
        It is supposed to update the tableview after adding a record. But that does not happen either though the signal - slot works because the processing entering the Addview().

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

          Hi,

          You are calling open twice in a row, that's your the current problem.

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

          G 1 Reply Last reply
          2
          • SGaistS SGaist

            Hi,

            You are calling open twice in a row, that's your the current problem.

            G Offline
            G Offline
            gabor53
            wrote on last edited by
            #5

            Hi @SGaist
            I deleted the db.open() from

                qDebug() << "Open connection names in Addview: " << db.connectionNames ();
            
            //	db.open ();
            
                if(!db.open ())
                    {
                        qDebug() << "The database (db) is NOT open!";
            
                    }
            

            and it still says The database (db) is NOT open!

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

              You should print the error you get from QSqlDatabase.

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

              G 1 Reply Last reply
              1
              • SGaistS SGaist

                You should print the error you get from QSqlDatabase.

                G Offline
                G Offline
                gabor53
                wrote on last edited by
                #7

                @SGaist
                The error message:
                The database (db) is NOT open! QSqlError("", "Driver not loaded", "Driver not loaded")

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

                  One of the thing you should first clean is how you handle your database. Your open statement should go in the same if where you create add the database.

                  Also, does fileQstring change at any time ? Do you handle that case ?

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

                  G 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    One of the thing you should first clean is how you handle your database. Your open statement should go in the same if where you create add the database.

                    Also, does fileQstring change at any time ? Do you handle that case ?

                    G Offline
                    G Offline
                    gabor53
                    wrote on last edited by
                    #9

                    @SGaist
                    fileQString doesn't change. I keep using the same db.

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

                      In that case, why not just open the database in your main function and be done with it ?

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

                      G 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        In that case, why not just open the database in your main function and be done with it ?

                        G Offline
                        G Offline
                        gabor53
                        wrote on last edited by
                        #11

                        @SGaist
                        You mean the main function of MainWindow_

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

                          No, I mean main in your main.cpp

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

                          G 1 Reply Last reply
                          0
                          • SGaistS SGaist

                            No, I mean main in your main.cpp

                            G Offline
                            G Offline
                            gabor53
                            wrote on last edited by
                            #13

                            @SGaist
                            I did the following:

                            #include "mainwindow.h"
                            #include <QApplication>
                            
                            int main(int argc, char *argv[])
                            {
                                QApplication a(argc, argv);
                                MainWindow w;
                            
                                QSqlDatabase db;
                                QString fileQstring = "C:/Programming/Projects/FolkFriends_1_0/db.db";
                            
                            
                                db = QSqlDatabase::addDatabase ("QSQLITE");
                                db.setDatabaseName (fileQstring);
                                qDebug() << "Connection Display created in connection(). ";
                            
                                bool OK = db.open ();
                            
                                if(OK == true)
                                    {
                                        qDebug() << "The db (MainWindow) is open!";
                                    }
                                else
                                    {
                                        qDebug() << "The db (MainWindow) is not open!";
                                    }
                            
                                if(!db.open ())
                                    {
                                        qDebug() << "The database (db) is NOT open!" << db.lastError ();
                            
                                    }
                            
                                db.open();
                            
                                w.show();
                            
                                return a.exec();
                            }
                            
                            

                            I still get
                            The query is NOT active. QSqlError("", "Driver not loaded", "Driver not loaded")

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

                              What version of Qt are you using ?

                              Do you have write access to that location ?

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

                              G 1 Reply Last reply
                              0
                              • SGaistS SGaist

                                What version of Qt are you using ?

                                Do you have write access to that location ?

                                G Offline
                                G Offline
                                gabor53
                                wrote on last edited by
                                #15

                                @SGaist
                                Yes . I have write access. Using Qt 5.7 on Windows 10

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

                                  Are you experiencing that using Qt Creator to start your application ?

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

                                  G 1 Reply Last reply
                                  1
                                  • SGaistS SGaist

                                    Are you experiencing that using Qt Creator to start your application ?

                                    G Offline
                                    G Offline
                                    gabor53
                                    wrote on last edited by
                                    #17

                                    @SGaist
                                    It starts ok.

                                    G 1 Reply Last reply
                                    0
                                    • G gabor53

                                      @SGaist
                                      It starts ok.

                                      G Offline
                                      G Offline
                                      gabor53
                                      wrote on last edited by
                                      #18

                                      @gabor53
                                      I think the problem is that it first executes function Addview() from mainwindow.cpp. this crates the first half of the messages:
                                      QSqlQuery::exec: database not open
                                      The query is NOT active. QSqlError("", "Driver not loaded", "Driver not loaded")

                                      After mainwindow.cpp it executes main.cpp where I create the db connection and open the db. This gives normal messages:

                                      Connection Display created in main.cpp.
                                      The db (MainWindow) is open!

                                      Clearly the problem is that it tries to run the query before it opens the db. I just still don't know why it processes mainwindow.cpp first and main.cpp second.

                                      jsulmJ 1 Reply Last reply
                                      0
                                      • G gabor53

                                        @gabor53
                                        I think the problem is that it first executes function Addview() from mainwindow.cpp. this crates the first half of the messages:
                                        QSqlQuery::exec: database not open
                                        The query is NOT active. QSqlError("", "Driver not loaded", "Driver not loaded")

                                        After mainwindow.cpp it executes main.cpp where I create the db connection and open the db. This gives normal messages:

                                        Connection Display created in main.cpp.
                                        The db (MainWindow) is open!

                                        Clearly the problem is that it tries to run the query before it opens the db. I just still don't know why it processes mainwindow.cpp first and main.cpp second.

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

                                        @gabor53 Then move

                                        MainWindow w;
                                        

                                        after setting up the connection.

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

                                        G 1 Reply Last reply
                                        1
                                        • jsulmJ jsulm

                                          @gabor53 Then move

                                          MainWindow w;
                                          

                                          after setting up the connection.

                                          G Offline
                                          G Offline
                                          gabor53
                                          wrote on last edited by
                                          #20

                                          @jsulm
                                          This worked. Thank you.

                                          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