Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. Hi i have a problem with displaying specific data from database in QTableView.I am using sqlite database.User should select search method in comboBox than enter data in search Field.
Forum Updated to NodeBB v4.3 + New Features

Hi i have a problem with displaying specific data from database in QTableView.I am using sqlite database.User should select search method in comboBox than enter data in search Field.

Scheduled Pinned Locked Moved Solved Brainstorm
7 Posts 3 Posters 1.5k 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.
  • B Offline
    B Offline
    BeastBook
    wrote on last edited by
    #1

    QSqlQueryModel *modal = new QSqlQueryModel();

       conOpen();
       QSqlQuery *qry = new QSqlQuery(mydb);
       qry->prepare("Select ID,Name,Surname,ind from Student where '"+combo->currentText()+"' = '"+searchField->text()+"'");
       qry->exec();
       modal->setQuery(*qry);
       table->setModel(modal);
       conClose();
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      The thread title is not the place to put the full question. Please edit it to a summarised version and put your question in the thread body.

      On to your problem. Did you check that the database is properly opened ? That its path is correctly set ?
      There's no need to allocate your QSqlQuery object on the heap.
      You also don't check that your query is successful.

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

      B 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        The thread title is not the place to put the full question. Please edit it to a summarised version and put your question in the thread body.

        On to your problem. Did you check that the database is properly opened ? That its path is correctly set ?
        There's no need to allocate your QSqlQuery object on the heap.
        You also don't check that your query is successful.

        B Offline
        B Offline
        BeastBook
        wrote on last edited by
        #3

        @SGaist Everthing works fine with database ,relativ path ,inserting,deleting,updating and displaying entire database on table.But searching and displaying do not work.Sorry for my title a i am new on this forum.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You can edit it without any problem and fix that.

          As suggested, you should check that your query runs successfully and also that it returns something.

          Also, you should pass the query string directly to the model, there's no need for that intermediate QSqlQuery.

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

          B 1 Reply Last reply
          0
          • SGaistS SGaist

            You can edit it without any problem and fix that.

            As suggested, you should check that your query runs successfully and also that it returns something.

            Also, you should pass the query string directly to the model, there's no need for that intermediate QSqlQuery.

            B Offline
            B Offline
            BeastBook
            wrote on last edited by
            #5

            @SGaist I solve my problem for some reason works this way Select * from Student where
            ID = "+36+" and not working with with this "'+36+'" .I am little confused it worked with ' ' when i use for editing updating inserting but not for searching

            conOpen();
            QSqlQuery *qry = new QSqlQuery(mydb);

                   qry->prepare("Select * from Info where Datum="+a+"");
                   qry->exec();
                   qDebug() << qry;
                   modal->setQuery(*qry);
                   table->setModel(modal);
                   conClose();
            

            Thank you man.You are great

            JonBJ 1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              That why you should rather use binding values. You wouldn't have got that problem with them.

              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
              2
              • B BeastBook

                @SGaist I solve my problem for some reason works this way Select * from Student where
                ID = "+36+" and not working with with this "'+36+'" .I am little confused it worked with ' ' when i use for editing updating inserting but not for searching

                conOpen();
                QSqlQuery *qry = new QSqlQuery(mydb);

                       qry->prepare("Select * from Info where Datum="+a+"");
                       qry->exec();
                       qDebug() << qry;
                       modal->setQuery(*qry);
                       table->setModel(modal);
                       conClose();
                

                Thank you man.You are great

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @BeastBook

                ID = "+36+" and not working with with this "'+36+'" .I am little confused it worked with ' ' when i use for editing updating inserting but not for searching
                

                Your ID column holds an integer, not a SQL string. You should not be quoting it (inside ''s) whether in a WHERE or INSERT or UPDATE, then you won't have consistency issues.

                1 Reply Last reply
                1

                • Login

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