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. Some filters in QSqlTableModel and SQLite
Forum Updated to NodeBB v4.3 + New Features

Some filters in QSqlTableModel and SQLite

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 5 Posters 4.1k 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.
  • M Offline
    M Offline
    Mikeeeeee
    wrote on last edited by
    #1

    Hi!
    There is this code:

    QSqlDatabase SearchDB = QSqlDatabase::addDatabase("QSQLITE");
        SearchDB.setDatabaseName("C:\\CVReader\\ResumeArraySQLite.db");
    
        QSqlTableModel *SearchTableModel = new QSqlTableModel(this);
        SearchTableModel ->setTable("SQLliteResumeArray");
    

    I need to make some filters.
    If I use such code only last filter performs:

    SearchTableModel->setFilter(" Age > 30 ");
    SearchTableModel->setFilter(" Age < 40 ");
    

    What code should I use to make two or more filters perform at the same time?
    May be I should to use QSortFilterProxyModel?

    jsulmJ 1 Reply Last reply
    0
    • M Mikeeeeee

      Hi!
      There is this code:

      QSqlDatabase SearchDB = QSqlDatabase::addDatabase("QSQLITE");
          SearchDB.setDatabaseName("C:\\CVReader\\ResumeArraySQLite.db");
      
          QSqlTableModel *SearchTableModel = new QSqlTableModel(this);
          SearchTableModel ->setTable("SQLliteResumeArray");
      

      I need to make some filters.
      If I use such code only last filter performs:

      SearchTableModel->setFilter(" Age > 30 ");
      SearchTableModel->setFilter(" Age < 40 ");
      

      What code should I use to make two or more filters perform at the same time?
      May be I should to use QSortFilterProxyModel?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Mikeeeeee As mentioned here http://doc.qt.io/qt-5/qsqltablemodel.html#setFilter
      "The filter is a SQL WHERE clause without the keyword WHERE", so you can do it same way you do it in a SQL query.

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

      1 Reply Last reply
      3
      • M Offline
        M Offline
        Mikeeeeee
        wrote on last edited by Mikeeeeee
        #3

        I understand this.
        How can I make few request?

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

          Hi,

          @Mikeeeeee said in Some filters in QSqlTableModel and SQLite:

          How can I make few request?

          What do you mean by that ?

          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
          0
          • M Mikeeeeee

            I understand this.
            How can I make few request?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Mikeeeeee "I understand this." - then you should know how to do it.
            Like explained here https://www.w3schools.com/sql/sql_and_or.asp

            SearchTableModel->setFilter(" Age > 30 AND Age < 40");
            

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

            1 Reply Last reply
            1
            • M Offline
              M Offline
              Mikeeeeee
              wrote on last edited by Mikeeeeee
              #6

              I understand this.
              But I have many composite filters.
              I need to make many separated filters.

              jsulmJ JonBJ 2 Replies Last reply
              0
              • M Offline
                M Offline
                Mikeeeeee
                wrote on last edited by
                #7

                May be I ned to use temp table in SQLite?

                1 Reply Last reply
                0
                • M Mikeeeeee

                  I understand this.
                  But I have many composite filters.
                  I need to make many separated filters.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Mikeeeeee said in Some filters in QSqlTableModel and SQLite:

                  composite filters

                  Can you give an example?
                  Why can't you do it like in my example?

                  "May be I ned to use temp table in SQLite?" - maybe you could use SQL views?

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

                  1 Reply Last reply
                  0
                  • M Mikeeeeee

                    I understand this.
                    But I have many composite filters.
                    I need to make many separated filters.

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

                    @Mikeeeeee
                    If you want several filters, you offer several filters to your user. If you want them them to be ANDed together, you build your SQL filter using AND between each one, like @jsulm said. It's your job to build up the necessary WHERE clause from whatever filtering you want. Does that answer your question?

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      Mikeeeeee
                      wrote on last edited by
                      #10

                      It's not answer.
                      Thea example of my filters:
                      TableModel->setFilter("A1 = 30 AND A2 = 3 AND A3 = 7");
                      TableModel->setFilter("A4 = 2 OR A5 = 2 OR A6 = 2");
                      TableModel->setFilter("A7 = 3 OR A8 = 3 OR A8 = 3");
                      TableModel->setFilter("A9 = 5 OR A10 = 5 OR A11 = 5");

                      jsulmJ 1 Reply Last reply
                      0
                      • M Mikeeeeee

                        It's not answer.
                        Thea example of my filters:
                        TableModel->setFilter("A1 = 30 AND A2 = 3 AND A3 = 7");
                        TableModel->setFilter("A4 = 2 OR A5 = 2 OR A6 = 2");
                        TableModel->setFilter("A7 = 3 OR A8 = 3 OR A8 = 3");
                        TableModel->setFilter("A9 = 5 OR A10 = 5 OR A11 = 5");

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Mikeeeeee You can combine them into one

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

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          Mikeeeeee
                          wrote on last edited by
                          #12

                          This variant will be slowly.

                          Christian EhrlicherC jsulmJ 2 Replies Last reply
                          0
                          • M Mikeeeeee

                            This variant will be slowly.

                            Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by Christian Ehrlicher
                            #13

                            @Mikeeeeee said in Some filters in QSqlTableModel and SQLite:

                            This variant will be slowly.

                            Why should filtering by the database be slower than doing it in c++?

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            1 Reply Last reply
                            3
                            • M Mikeeeeee

                              This variant will be slowly.

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @Mikeeeeee said in Some filters in QSqlTableModel and SQLite:

                              This variant will be slowly.

                              You measured it? Or how do you know it will be slow?
                              If you use SQL you should do the filtering in SQL, this is what SQL is for. And SQL server (and SQLite) can optimise SQL queries quite good. If you do the filtering in several steps in C++ this will happen: first you filter using one filter - a big amount of data will be loaded in your app (everything matching the first WHERE criteria), then you will apply second filter and so on. Doing complete filtering in SQL will be different: you will only load data in your app which matches all filters - way less memory consumption and faster operation.

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

                              1 Reply Last reply
                              2
                              • M Offline
                                M Offline
                                Mikeeeeee
                                wrote on last edited by
                                #15

                                thank you

                                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