Mariadb query returns empty strings



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


  • Lifetime Qt Champion

    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.



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



  • @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.


  • Lifetime Qt Champion

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



  • @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.


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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.