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

QSqlQuery::prepare: database not open

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 1.2k Views 1 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.
  • Ketan__Patel__0011K Offline
    Ketan__Patel__0011K Offline
    Ketan__Patel__0011
    wrote on last edited by
    #1

    Hello Friends

    My Qt version is : 5.12.3
    MYSQL version is : MySQL Server 5.6

    I am get a differnt type error when i am using MYSQL with qt

    when i try to load data in QComboBox that time following error are occurred.

    QSqlQuery::prepare: database not open
    QSqlError("", "Driver not loaded", "Driver not loaded")
    ("QSQLITE", "QMYSQL", "QMYSQL3", "QODBC", "QODBC3", "QPSQL", "QPSQL7")

    i did't get any solution please help me to solve this problem

    jsulmJ 1 Reply Last reply
    0
    • J JSher

      @SGaist Not to hijack this thread buy why not keep a class member QSqlDatabase variable? I do not see that in the https://doc.qt.io/qt-5/sql-connecting.html docs unless I missed it. I do it in my program without issues. I only use one db so there is no need for one I know however it was just code from when I was learning and I am just curious as to why this is wrong?

      @Ketan__Patel__0011
      You would have to show your code again now that you have it working but I guessing you close the db and do not reopen it?

      --James

      Ketan__Patel__0011K Offline
      Ketan__Patel__0011K Offline
      Ketan__Patel__0011
      wrote on last edited by
      #11

      @JSher

      I have solved this problem

      Thank To all

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JSher
        wrote on last edited by
        #2

        @Ketan__Patel__0011

        Is it only this code? if so show the code please.

        If its trying to connect in general debug your module trace and see what module it is failing on will give you an idea what module you may need to add/move. Make sure your libmysql.dll is in the right place compared to what compiler you are using.

        --James

        Ketan__Patel__0011K 1 Reply Last reply
        0
        • Ketan__Patel__0011K Ketan__Patel__0011

          Hello Friends

          My Qt version is : 5.12.3
          MYSQL version is : MySQL Server 5.6

          I am get a differnt type error when i am using MYSQL with qt

          when i try to load data in QComboBox that time following error are occurred.

          QSqlQuery::prepare: database not open
          QSqlError("", "Driver not loaded", "Driver not loaded")
          ("QSQLITE", "QMYSQL", "QMYSQL3", "QODBC", "QODBC3", "QPSQL", "QPSQL7")

          i did't get any solution please help me to solve this problem

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

          @Ketan__Patel__0011 This issue was asked many time here - please use search function.

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

          1 Reply Last reply
          0
          • J JSher

            @Ketan__Patel__0011

            Is it only this code? if so show the code please.

            If its trying to connect in general debug your module trace and see what module it is failing on will give you an idea what module you may need to add/move. Make sure your libmysql.dll is in the right place compared to what compiler you are using.

            --James

            Ketan__Patel__0011K Offline
            Ketan__Patel__0011K Offline
            Ketan__Patel__0011
            wrote on last edited by Ketan__Patel__0011
            #4

            @JSher

            i am using MSVC2015 kit

            Yes, libmysql.dll is it in proper and right place

            even i can perform any mysql query like insert, update or delete

            My Code Is :

                               void MainWindow::Populate_User_Combo(QComboBox *combo)                       
                               {                       
                                   Connect_DB();                       
                                   QSqlQueryModel *model = new QSqlQueryModel();                       
                                   QSqlQuery Query_load_camera_names(db);                       
                                   Query_load_camera_names.prepare("SELECT Names FROM User_Master");                       
                                   Query_load_camera_names.exec();                       
                                   model->setQuery(Query_load_camera_names);                       
                                   combo->setModel(model);                       
                                   Disconnect_DB();                       
                               }
            
                               void MainWindow::Connect_DB()
                                {
                                    if(db.isOpen() == true)
                                    {
                                        db.close();
                                    }
                                   else
                                    {
                                       qDebug() << db.lastError();
                                        qDebug() << QSqlDatabase::drivers();
                                        db.open();
                                        qDebug() << "Database Is Open : " << db.isOpen();
                                    }
                                }
            
                                void MainWindow::Disconnect_DB()
                                {
                                    db.close();
                                    qDebug() << "Database Is Disconnected";
                                }
            
            1 Reply Last reply
            0
            • J Offline
              J Offline
              JSher
              wrote on last edited by
              #5

              @Ketan__Patel__0011

              So if I understand you correctly you can insert/update/delete no problem but this code you posted is the problem:

              perhaps im missing something but it appears you call Connect_DB(); then the first line of it you:

              if(db.isOpen() == true)
               {
                       db.close();
               }
              

              close the db before trying to access it for your combo box

              --James

              Ketan__Patel__0011K 1 Reply Last reply
              0
              • J JSher

                @Ketan__Patel__0011

                So if I understand you correctly you can insert/update/delete no problem but this code you posted is the problem:

                perhaps im missing something but it appears you call Connect_DB(); then the first line of it you:

                if(db.isOpen() == true)
                 {
                         db.close();
                 }
                

                close the db before trying to access it for your combo box

                --James

                Ketan__Patel__0011K Offline
                Ketan__Patel__0011K Offline
                Ketan__Patel__0011
                wrote on last edited by
                #6

                @JSher

                Hey now a new problem when i call void MainWindow::Populate_User_Combo(QComboBox combo) first time it will work completely And Data are stored in QComboBox But when i call this function second time that 's it's not working

                and this errors are raised:
                QSqlQuery::prepare: database not open
                QSqlError("", "Driver not loaded", "Driver not loaded")
                ("QSQLITE", "QMYSQL", "QMYSQL3", "QODBC", "QODBC3", "QPSQL", "QPSQL7")

                help me to solve this problem

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

                  Hi,

                  You did not configure any connection.
                  Side point: do not keep a class member QSqlDatabase variable as explained in the documentation.

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

                  J 1 Reply Last reply
                  1
                  • SGaistS SGaist

                    Hi,

                    You did not configure any connection.
                    Side point: do not keep a class member QSqlDatabase variable as explained in the documentation.

                    J Offline
                    J Offline
                    JSher
                    wrote on last edited by
                    #8

                    @SGaist Not to hijack this thread buy why not keep a class member QSqlDatabase variable? I do not see that in the https://doc.qt.io/qt-5/sql-connecting.html docs unless I missed it. I do it in my program without issues. I only use one db so there is no need for one I know however it was just code from when I was learning and I am just curious as to why this is wrong?

                    @Ketan__Patel__0011
                    You would have to show your code again now that you have it working but I guessing you close the db and do not reopen it?

                    --James

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

                      @JSher In the QSqlDatabase details. See the first warning.

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

                      J 1 Reply Last reply
                      1
                      • SGaistS SGaist

                        @JSher In the QSqlDatabase details. See the first warning.

                        J Offline
                        J Offline
                        JSher
                        wrote on last edited by
                        #10

                        @SGaist Ahh gotcha thanks":)

                        --James

                        1 Reply Last reply
                        1
                        • J JSher

                          @SGaist Not to hijack this thread buy why not keep a class member QSqlDatabase variable? I do not see that in the https://doc.qt.io/qt-5/sql-connecting.html docs unless I missed it. I do it in my program without issues. I only use one db so there is no need for one I know however it was just code from when I was learning and I am just curious as to why this is wrong?

                          @Ketan__Patel__0011
                          You would have to show your code again now that you have it working but I guessing you close the db and do not reopen it?

                          --James

                          Ketan__Patel__0011K Offline
                          Ketan__Patel__0011K Offline
                          Ketan__Patel__0011
                          wrote on last edited by
                          #11

                          @JSher

                          I have solved this problem

                          Thank To all

                          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