Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. SQL WHERE Clause In QSqlQuery::setQuery( )
QtWS25 Last Chance

SQL WHERE Clause In QSqlQuery::setQuery( )

Scheduled Pinned Locked Moved Solved Qt 6
qsqlqueryqsqlquerymodeltableview
11 Posts 4 Posters 1.1k 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.
  • D Offline
    D Offline
    Donald9307
    wrote on 15 May 2023, 23:26 last edited by Donald9307
    #1

    I have a SQLITE3 database connected to my Qt6 application. I can pass a SQL SELECT statement to setQuery( ) and have the query execute as long as the statement doesn’t contain a WHERE clause with a variable expression. For example, if the SQL statement has a WHERE clause something like,

    SELECT * FROM table_name WHERE col-name = 3;

    If my query name is query, I can pass the query to my model and It works just fine. But if the col_name isn’t hard-coded into the statement, something like

    SELECT * FROM table_name WHERE col-name = var;

    in which var is the name of a variable, query.exec( ) fails.

    I want to be able to modify my TableView by using the CurrentText( ) of a comboBox which can be changed while the program is running so that my TableView only shows a subset of the table records based on the SQL WHERE clause.

    Does anyone know how you can use a variable in a setQuery( ) function containing a SQL WHERE clause in Qt C++?

    J P 2 Replies Last reply 16 May 2023, 07:49
    0
    • D Donald9307
      15 May 2023, 23:26

      I have a SQLITE3 database connected to my Qt6 application. I can pass a SQL SELECT statement to setQuery( ) and have the query execute as long as the statement doesn’t contain a WHERE clause with a variable expression. For example, if the SQL statement has a WHERE clause something like,

      SELECT * FROM table_name WHERE col-name = 3;

      If my query name is query, I can pass the query to my model and It works just fine. But if the col_name isn’t hard-coded into the statement, something like

      SELECT * FROM table_name WHERE col-name = var;

      in which var is the name of a variable, query.exec( ) fails.

      I want to be able to modify my TableView by using the CurrentText( ) of a comboBox which can be changed while the program is running so that my TableView only shows a subset of the table records based on the SQL WHERE clause.

      Does anyone know how you can use a variable in a setQuery( ) function containing a SQL WHERE clause in Qt C++?

      J Offline
      J Offline
      JonB
      wrote on 16 May 2023, 07:49 last edited by JonB
      #2

      @Donald9307
      SELECT statements with WHERE work just fine. And you can have WHERE col-name = var, so long as col-name is a literal (correct) column while var can be bound to a variable.

      Show your code where you specify and build this SELECT * FROM table_name WHERE col-name = var; and the return result or any error from executing it.

      But if the col_name isn’t hard-coded into the statement

      Not sure what you mean here. You do know which column you are searching so it should be hard-coded into the statement. And needs to be. You can bind the var variable to take on the value from your combobox, but you cannot have a "variable" for the column name.

      1 Reply Last reply
      1
      • D Donald9307
        15 May 2023, 23:26

        I have a SQLITE3 database connected to my Qt6 application. I can pass a SQL SELECT statement to setQuery( ) and have the query execute as long as the statement doesn’t contain a WHERE clause with a variable expression. For example, if the SQL statement has a WHERE clause something like,

        SELECT * FROM table_name WHERE col-name = 3;

        If my query name is query, I can pass the query to my model and It works just fine. But if the col_name isn’t hard-coded into the statement, something like

        SELECT * FROM table_name WHERE col-name = var;

        in which var is the name of a variable, query.exec( ) fails.

        I want to be able to modify my TableView by using the CurrentText( ) of a comboBox which can be changed while the program is running so that my TableView only shows a subset of the table records based on the SQL WHERE clause.

        Does anyone know how you can use a variable in a setQuery( ) function containing a SQL WHERE clause in Qt C++?

        P Offline
        P Offline
        Pl45m4
        wrote on 16 May 2023, 14:01 last edited by Pl45m4
        #3

        @Donald9307 said in SQL WHERE Clause In QSqlQuery::setQuery( ):

        Does anyone know how you can use a variable in a setQuery( ) function containing a SQL WHERE clause in Qt C++?

        With bindings, as mentioned by @JonB , it would look like this:

        QString comboBoxText = yourComboBox->currentText();
        
        
        QSqlQuery query("SELECT * FROM table_name WHERE col-name = :colName");
        query.bindValue(":colName", comboBoxText);
        
        

        [Edit: Untested code]


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        J D 2 Replies Last reply 16 May 2023, 15:37
        0
        • P Pl45m4
          16 May 2023, 14:01

          @Donald9307 said in SQL WHERE Clause In QSqlQuery::setQuery( ):

          Does anyone know how you can use a variable in a setQuery( ) function containing a SQL WHERE clause in Qt C++?

          With bindings, as mentioned by @JonB , it would look like this:

          QString comboBoxText = yourComboBox->currentText();
          
          
          QSqlQuery query("SELECT * FROM table_name WHERE col-name = :colName");
          query.bindValue(":colName", comboBoxText);
          
          

          [Edit: Untested code]

          J Offline
          J Offline
          JonB
          wrote on 16 May 2023, 15:37 last edited by
          #4

          @Pl45m4
          Umm, did you actually test this?? I haven't used SQLite but would be surprised, nay astounded, if this is acceptable....

          C P 2 Replies Last reply 16 May 2023, 15:58
          0
          • J JonB
            16 May 2023, 15:37

            @Pl45m4
            Umm, did you actually test this?? I haven't used SQLite but would be surprised, nay astounded, if this is acceptable....

            C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 16 May 2023, 15:58 last edited by
            #5

            Actually binding a query will not help here since the op wants to change the bound value dynamically. This can not work because you've no access to the QSqlQuery after QSqlQueryModel::setQuery(). So either pass an updated query after the combobox change our use a QSortFilterProxyModel and change the filter after every change.

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

            J 1 Reply Last reply 16 May 2023, 18:05
            0
            • J JonB
              16 May 2023, 15:37

              @Pl45m4
              Umm, did you actually test this?? I haven't used SQLite but would be surprised, nay astounded, if this is acceptable....

              P Offline
              P Offline
              Pl45m4
              wrote on 16 May 2023, 16:04 last edited by Pl45m4
              #6

              @JonB said in SQL WHERE Clause In QSqlQuery::setQuery( ):

              did you actually test this??

              No :) But IIRC at least binding (by name) works like this ;)

              @Christian-Ehrlicher I missed the model part, which @Donald9307 also forgot to mention :)


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              C J 2 Replies Last reply 16 May 2023, 16:36
              0
              • P Pl45m4
                16 May 2023, 16:04

                @JonB said in SQL WHERE Clause In QSqlQuery::setQuery( ):

                did you actually test this??

                No :) But IIRC at least binding (by name) works like this ;)

                @Christian-Ehrlicher I missed the model part, which @Donald9307 also forgot to mention :)

                C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 16 May 2023, 16:36 last edited by
                #7

                @Pl45m4 said in SQL WHERE Clause In QSqlQuery::setQuery( ):

                which @Donald9307 also forgot to mention :)

                It's just a wild guess due to 'I can pass a SQL SELECT statement to setQuery( )'

                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
                0
                • P Pl45m4
                  16 May 2023, 16:04

                  @JonB said in SQL WHERE Clause In QSqlQuery::setQuery( ):

                  did you actually test this??

                  No :) But IIRC at least binding (by name) works like this ;)

                  @Christian-Ehrlicher I missed the model part, which @Donald9307 also forgot to mention :)

                  J Offline
                  J Offline
                  JonB
                  wrote on 16 May 2023, 17:54 last edited by
                  #8

                  @Pl45m4 said in SQL WHERE Clause In QSqlQuery::setQuery( ):

                  No :) But IIRC at least binding (by name) works like this ;)

                  [Untested.] You cannot bind a table name to a variable in a query any more than you can bind a column name. You can bind a value for a column, e.g. in a WHERE clause or INSERT ... VALUES() statement. So not :table_name in yours.

                  1 Reply Last reply
                  1
                  • C Christian Ehrlicher
                    16 May 2023, 15:58

                    Actually binding a query will not help here since the op wants to change the bound value dynamically. This can not work because you've no access to the QSqlQuery after QSqlQueryModel::setQuery(). So either pass an updated query after the combobox change our use a QSortFilterProxyModel and change the filter after every change.

                    J Offline
                    J Offline
                    JonB
                    wrote on 16 May 2023, 18:05 last edited by JonB
                    #9

                    @Christian-Ehrlicher said in SQL WHERE Clause In QSqlQuery::setQuery( ):

                    wants to change the bound value dynamically. This can not work because you've no access to the QSqlQuery after QSqlQueryModel::setQuery(). So either pass an updated query after the combobox change

                    Yes, you have to call QSqlQueryModel::setQuery() afresh each time you want to change the filter value. Shame, but does it matter for this case? But OP can re-call same function which binds variable value. Of course if there a few rows/values it might be much simpler and better to read in and use QSortFilterProxyModel.

                    If you read carefully OP actually wrote

                    If my query name is query, I can pass the query to my model and It works just fine. But if the col_name isn’t hard-coded into the statement, something like
                    SELECT * FROM table_name WHERE col-name = var;
                    in which var is the name of a variable, query.exec( ) fails.

                    They state it is col_name they wish to have as variable/bind. That would mean the combobox would contain column names rather than possible values for a particular column. We need @Donald9307 to clarify this before the correct answer for the SQL statement can be given.

                    1 Reply Last reply
                    0
                    • P Pl45m4
                      16 May 2023, 14:01

                      @Donald9307 said in SQL WHERE Clause In QSqlQuery::setQuery( ):

                      Does anyone know how you can use a variable in a setQuery( ) function containing a SQL WHERE clause in Qt C++?

                      With bindings, as mentioned by @JonB , it would look like this:

                      QString comboBoxText = yourComboBox->currentText();
                      
                      
                      QSqlQuery query("SELECT * FROM table_name WHERE col-name = :colName");
                      query.bindValue(":colName", comboBoxText);
                      
                      

                      [Edit: Untested code]

                      D Offline
                      D Offline
                      Donald9307
                      wrote on 16 May 2023, 22:01 last edited by
                      #10

                      @Pl45m4 The bindings approach with my table column and variable worked!! Thanks for your help.

                      P 1 Reply Last reply 16 May 2023, 22:06
                      1
                      • D Donald9307 has marked this topic as solved on 16 May 2023, 22:03
                      • D Donald9307
                        16 May 2023, 22:01

                        @Pl45m4 The bindings approach with my table column and variable worked!! Thanks for your help.

                        P Offline
                        P Offline
                        Pl45m4
                        wrote on 16 May 2023, 22:06 last edited by Pl45m4
                        #11

                        @Donald9307

                        Just to clear the confusion, do you have any model or in which way do you use your query?
                        Because there is no QSqlQuery::setQuery() (as in the title).

                        But good to hear that it worked for you :)


                        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                        ~E. W. Dijkstra

                        1 Reply Last reply
                        0

                        1/11

                        15 May 2023, 23:26

                        • Login

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