Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Mariadb query returns empty strings
Forum Updated to NodeBB v4.3 + New Features

Mariadb query returns empty strings

Scheduled Pinned Locked Moved Solved C++ Gurus
7 Posts 3 Posters 1.7k 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.
  • G Offline
    G Offline
    Gosport_gaz
    wrote on 28 Jul 2018, 08:49 last edited by
    #1

    Hi
    I'm trying to get my head around model/view. Following a set of tutorials to create a Login form which then opens a second form, which allows the user to interact with the database.
    Having had success with a Sqlite database I decided to recreate the program using an identical database created in Mariadb(using the "QMYSQL" database connector). This works fine until I try to load data into a combobox and a listbox. Instead of a list of names I get a list containing 4 empty strings.

    I'm using Manjaro Linux
    My code is:
    Connection:
    bool connOpen()
    {

    // mydb = QSqlDatabase::addDatabase("QSQLITE");
    // mydb.setDatabaseName("/home/gary/dbhome/employeeTest.db");
    mydb = QSqlDatabase::addDatabase("QMYSQL");
    mydb.setHostName("localhost");
    mydb.setDatabaseName("employeeTest");
    mydb.setPassword("Password1");

        if(!mydb.open())
        {
            qDebug()<<"Failed to open database!!";
            return  false;
        }
        else
        {
            qDebug()<<"Connection OK";
            return  true;
        }
    }
    

    Action:
    void EmployeeInfo::on_btnLoad_clicked()
    {
    Login conn;
    QSqlQueryModel* model = new QSqlQueryModel();

    conn.connOpen();
    QSqlQuery* qry = new QSqlQuery(conn.mydb);
    qry->prepare("select name from employee_tbl");
    
    qry->exec();
    
    model -> setQuery(*qry);
    ui->lstNames->setModel(model);
    ui->cbxNames->setModel(model);
    
    conn.connClose();
    
    qDebug()<< model->rowCount();
    

    }
    the debug line gives an output of 4
    Any Ideas?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 28 Jul 2018, 20:05 last edited by
      #2

      Hi,

      You should use the QSqlQueryModel::lastError method. to know what is happening.

      From the looks of it, you should not be calling the exec method of your QSqlQuery object. Also, there's no need to allocate it on the heap. Take a look at the setQuery documentation.

      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 29 Jul 2018, 09:19
      1
      • S SGaist
        28 Jul 2018, 20:05

        Hi,

        You should use the QSqlQueryModel::lastError method. to know what is happening.

        From the looks of it, you should not be calling the exec method of your QSqlQuery object. Also, there's no need to allocate it on the heap. Take a look at the setQuery documentation.

        G Offline
        G Offline
        Gosport_gaz
        wrote on 29 Jul 2018, 09:19 last edited by
        #3

        @SGaist Hi thanks for the reply. I've tried using last error, but no error is being generated..

        G 1 Reply Last reply 29 Jul 2018, 09:34
        0
        • G Gosport_gaz
          29 Jul 2018, 09:19

          @SGaist Hi thanks for the reply. I've tried using last error, but no error is being generated..

          G Offline
          G Offline
          Gosport_gaz
          wrote on 29 Jul 2018, 09:34 last edited by
          #4

          @Gosport_gaz You're right about using the query exec method. The script works by setting the query as an argument to the setModel method. However the result is the same.

          J 1 Reply Last reply 30 Jul 2018, 07:04
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 29 Jul 2018, 17:47 last edited by
            #5

            By the way, why are you closing the connection to the database ?

            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
            • G Gosport_gaz
              29 Jul 2018, 09:34

              @Gosport_gaz You're right about using the query exec method. The script works by setting the query as an argument to the setModel method. However the result is the same.

              J Offline
              J Offline
              JonB
              wrote on 30 Jul 2018, 07:04 last edited by
              #6

              @Gosport_gaz
              If I were you and needed to debug this:

              • Don't call qry->exec(); prior to QComboBox::setModel().
              • Don't call conn.connClose(); prior to outputting model->rowCount(); (just in case).
              • You are sharing the same model across 2 widgets. Comment one of them out (just in case).
              • Use debugger/print statements to see what is actually being returned by the SELECT statement.
              1 Reply Last reply
              0
              • G Offline
                G Offline
                Gosport_gaz
                wrote on 30 Jul 2018, 08:16 last edited by
                #7

                Thanks JonB, It seems that connClose() is the problem. Or more specifically the line 'mydb.removeDatabase(QSqlDatabase::defaultConnection);'. It seems that this only affects the server/client database (as I've tried it with PostresSql with the same results.)
                I'm not sure why this doesn't affect Sqlite.

                1 Reply Last reply
                0

                1/7

                28 Jul 2018, 08:49

                • Login

                • Login or register to search.
                1 out of 7
                • First post
                  1/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved