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.



  • 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();

  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

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



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


Log in to reply