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. Problem with setFilter(), sql lite
QtWS25 Last Chance

Problem with setFilter(), sql lite

Scheduled Pinned Locked Moved Unsolved General and Desktop
sqllitesql database
6 Posts 3 Posters 865 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.
  • P Offline
    P Offline
    Pato55
    wrote on 30 Aug 2021, 09:11 last edited by Pato55
    #1

    Hi I have problem with my project, I have database and I don't know how to display in QTableView only what I want to find in database. I have button for adding and for deleting too, but button for finding don't work.

    this is my code
    query = new QSqlQuery(db);
    query->exec("CREATE TABLE IF NOT EXISTS Filmy (Jméno TEXT,Žánr TEXT,Hodnocení INTEGER,Poznámky TEXT, PRIMARY KEY(Jméno));");

    model = new QSqlTableModel(this, db);
    
    model->setTable("Filmy");
    
    model->select();
    model->setHeaderData(0, Qt::Horizontal, "Název");
    model->setHeaderData(1, Qt::Horizontal, "Žánr");
    model->setHeaderData(2, Qt::Horizontal, "Hodnocení");
    model->setHeaderData(3, Qt::Horizontal, "Poznámky");
    model->setSort(2, Qt::DescendingOrder);
    ui->tableView->resizeColumnsToContents();
    ui->tableView->horizontalHeader()->setSectionsClickable(true);
    ui->tableView->horizontalHeader()->setSectionsMovable(true);
    ui->tableView->setSortingEnabled(true);
    
    
    ui->tableView->setModel(model);
    

    this is function for search button

    void MainWindow::on_pushButton_3_clicked()
    { // vratim to co jsem napsal
    model = new QSqlTableModel(this, db);
    const QString edit = ui->lineEdit->text();
    model->setTable("Filmy");
    //qDebug()<<edit;
    model->setFilter("Název == Pád do tmy");
    //model->setFilter(QString("Název = '%1'").arg(edit));
    model->select();
    ui->tableView->setModel(model);

    I work in qt creator. Thank you for every response

    J 1 Reply Last reply 30 Aug 2021, 09:19
    0
    • P Pato55
      30 Aug 2021, 09:11

      Hi I have problem with my project, I have database and I don't know how to display in QTableView only what I want to find in database. I have button for adding and for deleting too, but button for finding don't work.

      this is my code
      query = new QSqlQuery(db);
      query->exec("CREATE TABLE IF NOT EXISTS Filmy (Jméno TEXT,Žánr TEXT,Hodnocení INTEGER,Poznámky TEXT, PRIMARY KEY(Jméno));");

      model = new QSqlTableModel(this, db);
      
      model->setTable("Filmy");
      
      model->select();
      model->setHeaderData(0, Qt::Horizontal, "Název");
      model->setHeaderData(1, Qt::Horizontal, "Žánr");
      model->setHeaderData(2, Qt::Horizontal, "Hodnocení");
      model->setHeaderData(3, Qt::Horizontal, "Poznámky");
      model->setSort(2, Qt::DescendingOrder);
      ui->tableView->resizeColumnsToContents();
      ui->tableView->horizontalHeader()->setSectionsClickable(true);
      ui->tableView->horizontalHeader()->setSectionsMovable(true);
      ui->tableView->setSortingEnabled(true);
      
      
      ui->tableView->setModel(model);
      

      this is function for search button

      void MainWindow::on_pushButton_3_clicked()
      { // vratim to co jsem napsal
      model = new QSqlTableModel(this, db);
      const QString edit = ui->lineEdit->text();
      model->setTable("Filmy");
      //qDebug()<<edit;
      model->setFilter("Název == Pád do tmy");
      //model->setFilter(QString("Název = '%1'").arg(edit));
      model->select();
      ui->tableView->setModel(model);

      I work in qt creator. Thank you for every response

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 30 Aug 2021, 09:19 last edited by
      #2

      @Pato55 said in Problem with setFilter(), sql lite:

      model->setFilter("Název == Pád do tmy");

      What about:

      model->setFilter("Název = 'Pád do tmy'");
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      P 1 Reply Last reply 30 Aug 2021, 09:30
      0
      • J jsulm
        30 Aug 2021, 09:19

        @Pato55 said in Problem with setFilter(), sql lite:

        model->setFilter("Název == Pád do tmy");

        What about:

        model->setFilter("Název = 'Pád do tmy'");
        
        P Offline
        P Offline
        Pato55
        wrote on 30 Aug 2021, 09:30 last edited by
        #3

        @jsulm No, It doesn't work. I don't why

        J 1 Reply Last reply 30 Aug 2021, 09:32
        0
        • P Pato55
          30 Aug 2021, 09:30

          @jsulm No, It doesn't work. I don't why

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 30 Aug 2021, 09:32 last edited by jsulm
          #4

          @Pato55 From documentation (https://doc.qt.io/qt-5/qsqltablemodel.html#setFilter): "The filter is a SQL WHERE clause without the keyword WHERE". But in your create table query there is no column called "Název", so how should this work?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          P 1 Reply Last reply 30 Aug 2021, 09:43
          2
          • J jsulm
            30 Aug 2021, 09:32

            @Pato55 From documentation (https://doc.qt.io/qt-5/qsqltablemodel.html#setFilter): "The filter is a SQL WHERE clause without the keyword WHERE". But in your create table query there is no column called "Název", so how should this work?

            P Offline
            P Offline
            Pato55
            wrote on 30 Aug 2021, 09:43 last edited by
            #5

            @jsulm Yes, I created table with bad name, thank you for saving my study life :)

            P 1 Reply Last reply 30 Aug 2021, 18:08
            0
            • P Pato55
              30 Aug 2021, 09:43

              @jsulm Yes, I created table with bad name, thank you for saving my study life :)

              P Offline
              P Offline
              Pablo J. Rogina
              wrote on 30 Aug 2021, 18:08 last edited by
              #6

              @Pato55 said in Problem with setFilter(), sql lite:

              thank you for saving my study life

              If your issue is solved, please don't forget to mark your post as such!

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0

              1/6

              30 Aug 2021, 09:11

              • Login

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