QTableWidget not showing SQL Query.

  • Hi! Good afternoon (atleast in spain haha) Well, I'm having a little problem with QTableWidget displaying users from my SQLITE 3 table (usuarios). Hope you can help me.

    Here's my query code:

    QSqlQuery query;
    query.exec ("SELECT * FROM usuarios WHERE Nombre=nombre AND Curso=curso AND Grupo=grupo");

    Here's my QTableWidget code:

        int row = 0;
        QTableWidgetItem *tmpItem;
        while (query.next()) {
        int col = 0;
        tmpItem= new QTableWidgetItem(tr("%1").arg(query.value(0).toString()));
        ui->tableWidget->setItem(row, col++, tmpItem);
        tmpItem= new QTableWidgetItem(tr("%1").arg(query.value(1).toString()));
        ui->tableWidget->setItem(row, col++, tmpItem);
        tmpItem = new QTableWidgetItem(tr("%1").arg(query.value(2).toString()));
        ui->tableWidget->setItem(row, col++, tmpItem);
        tmpItem = new QTableWidgetItem(tr("%1").arg(query.value(3).toString()));
        ui->tableWidget->setItem(row, col++, tmpItem);

    Screenshot of my SQLITE3 Database:


    Screenshot of my Table When the search (using the terms in the database)


  • Lifetime Qt Champion

    @cxam said:

    query.exec ("SELECT * FROM usuarios WHERE Nombre=nombre AND Curso=curso AND Grupo=grupo");

    this is not correct SQL i think.

    try with
    query.exec ("SELECT * FROM usuarios");
    to see if it will display.

  • Lifetime Qt Champion


    If you want to select only these three fields then you have to use a query like:

    "SELECT nombre AS Nombre, curso AS Curso , grupo=Grupo FROM usuarios"

    Hope it helps

  • @SGaist in SQL everything is case insensitive. The normal use would be

    "SELECT nombre, curso, grupo FROM usuarios;"

    The semicolon is also needed by SQL, although I have seen ODBC drivers that would choke if it was there.

    Except for these picky details, your answer is correct though.

  • @SGaist said:

    SELECT nombre AS Nombre, curso AS Curso , grupo=Grupo FROM usuarios

    It doesn't show anything, I've qDebug()'ed the variable to show if the output was matching the data to be searched on the SQL and indeed the data to be searched is correct:

    Nombre =  "Paco Cansalada"
    Curso =  "1 Batx"
    Grupo =  "A"

    I also tried to show everything on the database with "SELECT * FROM usuarios) to see if the problem was in the SQL statement but the database didn't display anything so I'm afraid the problem might be in the part where the SQL information is printed in the table.


  • Lifetime Qt Champion

    @mjsurette I was suggesting to use aliases in order to have the headers directly with the right casing.

    @cxam Did you check that the connection is successfully opened ? That the query executed successfully ?

  • @SGaist Yes, The connection is oppened properly. How do I check that the query works properly ?

  • Lifetime Qt Champion

    if( !query.exec(...) )
    qDebug() << query.lastError().text();

  • @mrjj Ok so, it seems that it doesn't detect the table "usuarios" even though it's there...

    So in my sqlite editor I executed the command and it worked properly:

    EDIT: I solved that problem: the program was searching the db in the remote directory not in the real directory, now it doesn't says any error but It doesn't shows the information.

    I've tried (SELECT * FROM usuarios) but nothing, it doesn't works.

    2ND EDIT: I solved the problem, it was on my printing method, I tried a much simpler methor by doing:

    int i=0;
        while (query.next()) {
            ui->tableWidget->setItem(i,0,new QTableWidgetItem(query.value(0).toString()));
            ui->tableWidget->setItem(i,1,new QTableWidgetItem(query.value(1).toString()));
            ui->tableWidget->setItem(i,2,new QTableWidgetItem(query.value(2).toString()));

    Thank you for your help.

  • Banned

    This post is deleted!