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
Qt 6.11 is out! See what's new in the release blog

db is not open when called the second time

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 4 Posters 8.0k 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.
  • 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