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. QTableWidget not showing SQL Query.

QTableWidget not showing SQL Query.

Scheduled Pinned Locked Moved Solved General and Desktop
qtablewidgetsqlqueryselect
10 Posts 5 Posters 6.2k 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.
  • C Offline
    C Offline
    cxam
    wrote on 1 Apr 2016, 22:35 last edited by
    #1

    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:

    ui->tableWidget->setColumnCount(3);
        ui->tableWidget->setRowCount(query.size());
        ui->tableWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
        ui->tableWidget->setHorizontalHeaderLabels(QString("Nombre;Curso;Grupo").split(";"));
    
        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);
    
        row++;
        }
    

    Screenshot of my SQLITE3 Database:

    http://s24.postimg.org/u6qd3neol/screen1.png

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

    http://s16.postimg.org/ssscc7r5x/screen2.png

    Stay Hungry, Stay Foolish

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 1 Apr 2016, 22:41 last edited by
      #2

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

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 1 Apr 2016, 22:49 last edited by
        #3

        Hi,

        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

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

        M C 2 Replies Last reply 2 Apr 2016, 06:59
        0
        • S SGaist
          1 Apr 2016, 22:49

          Hi,

          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

          M Offline
          M Offline
          mjsurette
          wrote on 2 Apr 2016, 06:59 last edited by
          #4

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

          1 Reply Last reply
          0
          • S SGaist
            1 Apr 2016, 22:49

            Hi,

            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

            C Offline
            C Offline
            cxam
            wrote on 2 Apr 2016, 07:34 last edited by
            #5

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

            Regards.

            Stay Hungry, Stay Foolish

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 2 Apr 2016, 20:43 last edited by
              #6

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

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

              C 1 Reply Last reply 3 Apr 2016, 09:08
              0
              • S SGaist
                2 Apr 2016, 20:43

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

                C Offline
                C Offline
                cxam
                wrote on 3 Apr 2016, 09:08 last edited by
                #7

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

                Stay Hungry, Stay Foolish

                M 1 Reply Last reply 3 Apr 2016, 09:34
                0
                • C cxam
                  3 Apr 2016, 09:08

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

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 3 Apr 2016, 09:34 last edited by
                  #8

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

                  C 1 Reply Last reply 3 Apr 2016, 10:25
                  0
                  • M mrjj
                    3 Apr 2016, 09:34

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

                    C Offline
                    C Offline
                    cxam
                    wrote on 3 Apr 2016, 10:25 last edited by cxam 4 Mar 2016, 11:09
                    #9

                    @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:
                    http://s18.postimg.org/iqry91pmx/image.png

                    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()));
                            i++;
                            ui->tableWidget->insertRow(i);
                        }
                    

                    Thank you for your help.

                    Stay Hungry, Stay Foolish

                    1 Reply Last reply
                    1
                    • T Offline
                      T Offline
                      Tony Eden
                      Banned
                      wrote on 19 Apr 2022, 15:12 last edited by
                      #10
                      This post is deleted!
                      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