Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. MySQL doesn't return data
Qt 6.12 LTS is out! See what's new in the release blog

MySQL doesn't return data

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 2 Posters 3.0k Views
  • 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.
  • I Izba

    Hi i'm having problems with reading data out of database.

    The connection should be fine as bool ok = m_db.open() returns 1.

    i connect to database like this:

        m_db = QSqlDatabase::addDatabase("QMYSQL");
        m_db.setHostName(m_hostname);
        m_db.setUserName(m_username);
        m_db.setPort(m_port);
        m_db.setPassword(m_password);
        bool ok = m_db.open();
    

    Then i select dbname with other function:

    void SqlDB::SelectDB(QString &dbname){
        std::cout <<"SelectDB" << " " << dbname.toStdString() << std::endl;
        m_db.setDatabaseName(dbname);
    }
    

    And then i try to use other function to get the data.

        std::cout <<"GetTables" << std::endl;
        QSqlDriver *driver = m_db.driver();
        std::cout << "isopen:" << m_db.isOpen();
        std::cout << "db name=" <<m_db.databaseName().toStdString();
        QSqlRecord record = driver->record("auto");
        QSqlQuery q;
        q.exec("SELECT * from auto");
        while(q.next()){
            QString val;
            val = q.value(0).toString();
            std::cout << val.toStdString();
        }
        std::cout << "record count = " << record.count();
    

    isopen = 1,
    dbname is correct, but it still gives me record.count() = 0.

    I have also tried to use db.tables(), but it returns 0 lines too.

    Thanks,
    Izba

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

    @Izba What does http://doc.qt.io/qt-5/qsqlquery.html#lastError return?

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

    I 1 Reply Last reply
    0
    • jsulmJ jsulm

      @Izba What does http://doc.qt.io/qt-5/qsqlquery.html#lastError return?

      I Offline
      I Offline
      Izba
      wrote on last edited by
      #3

      @jsulm

          QSqlError err = m_db.lastError();
          std::cout << err.databaseText().toStdString();
          std::cout << err.text().toStdString();
          std::cout << err.driverText().toStdString();
      

      I cant get it to return anything. Maybe i'm trying to debug it wrong?

      jsulmJ 1 Reply Last reply
      0
      • I Izba

        @jsulm

            QSqlError err = m_db.lastError();
            std::cout << err.databaseText().toStdString();
            std::cout << err.text().toStdString();
            std::cout << err.driverText().toStdString();
        

        I cant get it to return anything. Maybe i'm trying to debug it wrong?

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

        @Izba I mean the error from QSqlQuery

        QSqlQuery q;
        q.exec("SELECT * from auto");
        while(q.next()){
            QString val;
            val = q.value(0).toString();
            std::cout << val.toStdString();
        }
        std::cout << "record count = " << record.count();
        qDebug() << q.lastError();
        

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

        I 1 Reply Last reply
        0
        • jsulmJ jsulm

          @Izba I mean the error from QSqlQuery

          QSqlQuery q;
          q.exec("SELECT * from auto");
          while(q.next()){
              QString val;
              val = q.value(0).toString();
              std::cout << val.toStdString();
          }
          std::cout << "record count = " << record.count();
          qDebug() << q.lastError();
          
          I Offline
          I Offline
          Izba
          wrote on last edited by
          #5

          @jsulm said in MySQL doesn't return data:

          val = q.value(0).toString();
          std::cout << val.toStdString();
          

          }
          std::cout << "record count = " << record.count();
          qDebug() << q.lastError();

          Oh yeah it gives me an error that no database selected, but m_db.databaseName still gives me correct database.

          I have set the database by m_db.setDatabaseName().

          Do i need to set it some other way that the query would recognize it?

          Thanks

          jsulmJ 1 Reply Last reply
          0
          • I Izba

            @jsulm said in MySQL doesn't return data:

            val = q.value(0).toString();
            std::cout << val.toStdString();
            

            }
            std::cout << "record count = " << record.count();
            qDebug() << q.lastError();

            Oh yeah it gives me an error that no database selected, but m_db.databaseName still gives me correct database.

            I have set the database by m_db.setDatabaseName().

            Do i need to set it some other way that the query would recognize it?

            Thanks

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

            @Izba Try to pass the db as parameter:

            QSqlQuery q(QSqlDatabase::database("YOURDBNAME"));
            

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

            I 1 Reply Last reply
            0
            • jsulmJ jsulm

              @Izba Try to pass the db as parameter:

              QSqlQuery q(QSqlDatabase::database("YOURDBNAME"));
              
              I Offline
              I Offline
              Izba
              wrote on last edited by
              #7

              @jsulm

                QSqlQuery q(QSqlDatabase::database("mottonen"));
                  q.exec("SELECT * from auto");
                  while(q.next()){
                      QString val;
                      val = q.value(0).toString();
                      std::cout << val.toStdString();
                  }
                  qDebug() << q.lastError();
              

              Still same error:

              QSqlError("1046", "QMYSQL: Kyselyn suorittaminen epäonnistui", "No database selected")
              
              jsulmJ 1 Reply Last reply
              0
              • I Izba

                @jsulm

                  QSqlQuery q(QSqlDatabase::database("mottonen"));
                    q.exec("SELECT * from auto");
                    while(q.next()){
                        QString val;
                        val = q.value(0).toString();
                        std::cout << val.toStdString();
                    }
                    qDebug() << q.lastError();
                

                Still same error:

                QSqlError("1046", "QMYSQL: Kyselyn suorittaminen epäonnistui", "No database selected")
                
                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @Izba

                QSqlQuery q(m_db);
                

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

                I 1 Reply Last reply
                0
                • jsulmJ jsulm

                  @Izba

                  QSqlQuery q(m_db);
                  
                  I Offline
                  I Offline
                  Izba
                  wrote on last edited by
                  #9

                  @jsulm

                  No change at all. Still the same error that database is not selected.

                  jsulmJ 1 Reply Last reply
                  0
                  • I Izba

                    @jsulm

                    No change at all. Still the same error that database is not selected.

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

                    @Izba Please read documentation:
                    "Sets the connection's database name to name. To have effect, the database name must be set before the connection is opened."
                    http://doc.qt.io/qt-5/qsqldatabase.html#setDatabaseName

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

                    I 1 Reply Last reply
                    2
                    • jsulmJ jsulm

                      @Izba Please read documentation:
                      "Sets the connection's database name to name. To have effect, the database name must be set before the connection is opened."
                      http://doc.qt.io/qt-5/qsqldatabase.html#setDatabaseName

                      I Offline
                      I Offline
                      Izba
                      wrote on last edited by
                      #11

                      @jsulm
                      Yeah thank you a lot that did it! Guess i have skipped that part.

                      thanks,
                      izba

                      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
                      • Unsolved