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 Unsolved General and Desktop
30 Posts 6 Posters 17.1k 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.
  • R RekTek249

    Hey, I started SQL today and I was wondering why it says db isn't open. Note: I had trouble with another error saying the driver wasn't found or something and I added a bunch of files to system PATH. Here is my code.

    void w::database(){
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
        db.setHostName("Host");
        db.setDatabaseName("Name");         /* In my real code, I set the real
        db.setUserName("Username");               stuff here */
        db.setPassword("Password");
    
        if (db.open())                                             
        {                                                                        
            QMessageBox msgBox;                         
            msgBox.setText("DB OPENED");          
            msgBox.exec();
    
        }
    
    
        QSqlQuery query(db);
        query.prepare("INSERT INTO person (Username, Password, summ, Email) "  
                          "VALUES (:user, :pass, :summ, :mail)");
        query.bindValue(":user", Username);          // I do have variables set earlier
        query.bindValue(":pass", Password);
        query.bindValue(":summ", summ);
        query.bindValue(":mail", Email);
        query.exec();
    
    }
    

    For some reasons, when I press the button (which launch this method) if I have the db.open part, It "doesn't respond". Help anyone... I searched on internet but didn't find anything that solved my problem...
    Thanks

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

    @RekTek249 said in QSqlQuery::prepare: database not open:

    "doesn't respond"

    is the host reachable? Did you try to connect to the server using MySQL client?

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

    R 1 Reply Last reply
    1
    • M mawh1960

      Hi,

      Are you possibly missing a call to setPort() ?

      Example:

      db.setPort(3306);
      
      R Offline
      R Offline
      RekTek249
      wrote on last edited by
      #4

      @mawh1960 I didn't know you had to set port... where to you know the port ? is 3306 the default one ?

      1 Reply Last reply
      0
      • jsulmJ jsulm

        @RekTek249 said in QSqlQuery::prepare: database not open:

        "doesn't respond"

        is the host reachable? Did you try to connect to the server using MySQL client?

        R Offline
        R Offline
        RekTek249
        wrote on last edited by
        #5

        @jsulm It works with phpmyadmin... It doesn't respond for like 30sec and then if I wait it sends the "database not open"message in the output

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

          Hi,

          Are you running phpmyadmin on the same machine as the MySQL server ?

          Are you running your application on the same machine as the server ?

          If no to the second question, then did you correctly configure your MySQL server to accept external connections ? And also don't forget to secure them.

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

          R 1 Reply Last reply
          1
          • SGaistS SGaist

            Hi,

            Are you running phpmyadmin on the same machine as the MySQL server ?

            Are you running your application on the same machine as the server ?

            If no to the second question, then did you correctly configure your MySQL server to accept external connections ? And also don't forget to secure them.

            R Offline
            R Offline
            RekTek249
            wrote on last edited by
            #7

            @SGaist I'm hosting the sql server on a website. It's a hosting website so I guess it's a linux server ? I'm running phpmyadmin on my windows 10 computer.

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

              Then check that you are correctly using the same user name, password as you are using in PHPMyAdmin.

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

              R 1 Reply Last reply
              0
              • SGaistS SGaist

                Then check that you are correctly using the same user name, password as you are using in PHPMyAdmin.

                R Offline
                R Offline
                RekTek249
                wrote on last edited by
                #9

                @SGaist It is the correct one i checked that before asking it here...

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

                  What exact error message do you get from your QSqlDatabase object ?

                  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
                  • R RekTek249

                    Hey, I started SQL today and I was wondering why it says db isn't open. Note: I had trouble with another error saying the driver wasn't found or something and I added a bunch of files to system PATH. Here is my code.

                    void w::database(){
                    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
                        db.setHostName("Host");
                        db.setDatabaseName("Name");         /* In my real code, I set the real
                        db.setUserName("Username");               stuff here */
                        db.setPassword("Password");
                    
                        if (db.open())                                             
                        {                                                                        
                            QMessageBox msgBox;                         
                            msgBox.setText("DB OPENED");          
                            msgBox.exec();
                    
                        }
                    
                    
                        QSqlQuery query(db);
                        query.prepare("INSERT INTO person (Username, Password, summ, Email) "  
                                          "VALUES (:user, :pass, :summ, :mail)");
                        query.bindValue(":user", Username);          // I do have variables set earlier
                        query.bindValue(":pass", Password);
                        query.bindValue(":summ", summ);
                        query.bindValue(":mail", Email);
                        query.exec();
                    
                    }
                    

                    For some reasons, when I press the button (which launch this method) if I have the db.open part, It "doesn't respond". Help anyone... I searched on internet but didn't find anything that solved my problem...
                    Thanks

                    Taz742T Offline
                    Taz742T Offline
                    Taz742
                    wrote on last edited by
                    #11

                    @RekTek249

                        QSqlQuery query(db);
                        query.prepare("INSERT INTO person (Username, Password, summ, Email) "  
                                          "VALUES (:user, :pass, :summ, :mail)");
                        query.bindValue(":user", Username);          // I do have variables set earlier
                        query.bindValue(":pass", Password);
                        query.bindValue(":summ", summ);
                        query.bindValue(":mail", Email);
                        query.exec();
                    

                    The mistake is not giving you anything error?

                    please #include "QSqlError" and

                     if (db.open())                                             
                        {                                                                        
                            QMessageBox msgBox;                         
                            msgBox.setText("DB OPENED");          
                            msgBox.exec();
                    
                        }
                    else{
                           qDebug() << db.lastError()
                     }
                    

                    By doing so we can understand what the problem is about anyway...

                    Do what you want.

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      RekTek249
                      wrote on last edited by RekTek249
                      #12

                      @Taz742 QSqlError("2003", "QMYSQL: Unable to connect", "Can't connect to MySQL server on 'adress.here' (10060)")
                      Is the error...

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

                        Did you check that the user you are using to connect to the database can be used for remote connections ?

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

                        R 1 Reply Last reply
                        1
                        • SGaistS SGaist

                          Did you check that the user you are using to connect to the database can be used for remote connections ?

                          R Offline
                          R Offline
                          RekTek249
                          wrote on last edited by
                          #14

                          @SGaist It can.... What is the "2003" and 10060 meaning ? is 2003 the port ?

                          Taz742T 1 Reply Last reply
                          0
                          • R Offline
                            R Offline
                            RekTek249
                            wrote on last edited by
                            #15

                            I tried it on another server and it gives the following error..

                            QSqlError("2005", "QMYSQL: Unable to connect", "Unknown MySQL server host 'mysql.hostinger.fr' (0)")
                            QSqlQuery::prepare: database not open
                            
                            the_T 1 Reply Last reply
                            0
                            • R RekTek249

                              I tried it on another server and it gives the following error..

                              QSqlError("2005", "QMYSQL: Unable to connect", "Unknown MySQL server host 'mysql.hostinger.fr' (0)")
                              QSqlQuery::prepare: database not open
                              
                              the_T Offline
                              the_T Offline
                              the_
                              wrote on last edited by
                              #16

                              @RekTek249
                              Again: are you sure you are allowed to establish a remote connection to the database?

                              • check user permission
                              • check if port is correct
                              • check if port is reachable from remote

                              -- No support in PM --

                              1 Reply Last reply
                              2
                              • R RekTek249

                                @SGaist It can.... What is the "2003" and 10060 meaning ? is 2003 the port ?

                                Taz742T Offline
                                Taz742T Offline
                                Taz742
                                wrote on last edited by
                                #17

                                @RekTek249 said in QSqlQuery::prepare: database not open:

                                It can.... What is the "2003" and 10060 meaning ? is 2003 the port ?

                                2003 is not a port name, so let's say the title of the problem.

                                Do what you want.

                                the_T 1 Reply Last reply
                                0
                                • Taz742T Taz742

                                  @RekTek249 said in QSqlQuery::prepare: database not open:

                                  It can.... What is the "2003" and 10060 meaning ? is 2003 the port ?

                                  2003 is not a port name, so let's say the title of the problem.

                                  the_T Offline
                                  the_T Offline
                                  the_
                                  wrote on last edited by
                                  #18

                                  @Taz742
                                  It's not a "title", just an qt internal error number. Same with the other code that is a mysql internal error code

                                  -- No support in PM --

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

                                    To add to @the_ : the error state that mysql.hostinger.fr is an unknown host. Looks like you are giving the wrong address.

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

                                    R 1 Reply Last reply
                                    0
                                    • SGaistS SGaist

                                      To add to @the_ : the error state that mysql.hostinger.fr is an unknown host. Looks like you are giving the wrong address.

                                      R Offline
                                      R Offline
                                      RekTek249
                                      wrote on last edited by
                                      #20

                                      @SGaist When I go to the "remote mysql" part of the site, it gives an access host... do i gotta use this as the hostname ?

                                      R 1 Reply Last reply
                                      0
                                      • R RekTek249

                                        @SGaist When I go to the "remote mysql" part of the site, it gives an access host... do i gotta use this as the hostname ?

                                        R Offline
                                        R Offline
                                        RekTek249
                                        wrote on last edited by RekTek249
                                        #21

                                        @RekTek249 If that's what i do, it gives that error:

                                        QSqlError("1045", "QMYSQL: Unable to connect", "Access denied for user 'rektek_write'@'*ip here*.' (using password: YES)")
                                        

                                        If it's a permission problem, what do I need to do ? I have the insert one because that's the only thing I need to do...

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

                                          Is this user authorised to access the database from outside that machine ?

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

                                          R 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