Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Search Logic
Qt 6.11 is out! See what's new in the release blog

Search Logic

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 415 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.
  • S Offline
    S Offline
    Stephen28
    wrote on last edited by
    #1

    can any one help me with how to write logic to search button. Qt based one.. I have stored value in back end database. and showing the search in table view widget of my gui. how can i write query if user ender and searching any of these one field or h=

    SELECT * FROM userdata WHERE (username LIKE '%' OR username LIKE 'Sam') AND (userrole LIKE 'Admin' OR userrole LIKE '%');

    I have tried to retrieve like this, i know it'll give all the values in the data.how can i write query for this?!search.png

    1 Reply Last reply
    0
    • E Offline
      E Offline
      Emre MUTLU
      wrote on last edited by
      #2
      QString nameField=ui->lineEdit_name->Text();
      QString comboBoxRole=ui->comboBox_role->currentText();
      QString salaryField=ui.lineEdit_salary->Text();
      

      Query:

      SELECT * FROM userdata WHERE(username LIKE'"+nameFiled+"%'  OR userrole LIKE '"+comboBoxRole+"%' OR salary LIKE'"+salaryField+"%' );
      

      you want like this ?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Stephen28
        wrote on last edited by
        #3

        QString UserNameS=ui->SearchBox_Name->text();
        QString UserRoleS = ui->SearchBox_Role->currentText();
        QString UserSalaryS= ui->SearchBox_Salary->text();

        if (database.open()) {
            QMessageBox ::information(this,"Connection","Database is connected");
            //        QMessageBox::information(this,"Cnnected","connected");
        
            this->model= new QSqlQueryModel();
            QSqlQuery* qry1= new QSqlQuery();
        
            qry1->prepare("SELECT * FROM userdata WHERE (username LIKE '%' OR username LIKE ':UserNameS') AND (userrole LIKE ':UserRoleS' OR userrole LIKE '%');"); //SELECT * FROM userdata WHERE username IN (:UserNameS,'') AND userrole IN (:UserRoleS,'')//SELECT * FROM userdata WHERE username IN (:UserNameS,'%') AND userrole LIKE (:UserRoleS,'%');
        
            qry1->bindValue(":UserNameS",UserNameS);
            qry1->bindValue(":UserRoleS",UserRoleS);
        
        
            if (!qry1->exec()) {
                QMessageBox::information(this,"Failed","Query Not Executed");
            }else {
        
                while(qry1->next()) {
                    QString UserNameFromDb =qry1->value(1).toString();
                    QString UserRoleFromDb=qry1->value(2).toString();
        
                    if ((UserNameFromDb==UserNameS) || (UserRoleFromDb==UserRoleS) ) {   // && (UserRoleFromDb==UserRoleS)
                        //qry1->exec();
                        model->setQuery(*qry1);
                        ui->SearchResult->setModel(model);
                    }else {
                        QMessageBox::information(this,"No User Found","No User Found in this name");
                    }
        
                }
        

        yes i want like this,but i dont know how to write query or logic for this

        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