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. QSqlTableModel setFilter => Unable to execute statement

QSqlTableModel setFilter => Unable to execute statement

Scheduled Pinned Locked Moved Solved General and Desktop
qsqltablemodelqsqldatabase
8 Posts 5 Posters 325 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.
  • M Offline
    M Offline
    msauer751
    wrote on 1 Feb 2025, 14:34 last edited by
    #1

    Hi,
    I use QSqlTableModel to access my sqlite database. If I want to select entrys with setFilter I will get the following error:

    >> Database > searchData ( 724 ) INFO lastError= QSqlError("1", "Unable to execute statement", "near \"group\": syntax error")
    

    My function to find data:

        QString CfgDb::cfgFindKey(const QString &groupArg, const QString &keyArg)
        {
            QJsonObject ret;
            errorId = 0;
            errorMsg = "";
    
            ret = searchData(QString::fromStdString(TBLNAME), "(group='" + groupArg + "') AND (key=" + keyArg + ")");
            qDebug() << ">> CfgDb > cfgChgKey (" << __LINE__ << ") INFO ret=" << ret;
            QString sret {};
            if (ret["anz"].toInt() == 0)
            {
                sret = "<not found>";
            }
            /*        else
                    {
                        QJsonArray array = ret[QString::fromStdString(TBLNAME)].toArray();
                        for (uint16_t i = 0; i < array.size(); ++i)
                        {
                            if (array[i])
                        }
                    }
            */
            return sret;
        }
    

    searchData:

        QJsonObject Database::searchData(const QString &tableNameArg, const QString &whereArg)
        {
            QJsonObject ret {};
            errorId = 0;
            errorMsg = "";
    
            QSqlTableModel *model {nullptr};
            QJsonArray array {};
            model = new QSqlTableModel(this, db);
            qDebug() << ">> Database > searchData (" << __LINE__ << ") INFO " << model->database();
            model->setTable(tableNameArg);
            model->setEditStrategy(QSqlTableModel::OnManualSubmit);
            model->setFilter(whereArg);
            qDebug() << ">> Database > searchData (" << __LINE__ << ") INFO tableNameArg=" << tableNameArg;
            qDebug() << ">> Database > searchData (" << __LINE__ << ") INFO whereArg=" << model->filter();
            model->select();
            qDebug() << ">> Database > searchData (" << __LINE__ << ") INFO select=" << model->select();
            qDebug() << ">> Database > searchData (" << __LINE__ << ") INFO lastError=" << model->lastError();
            uint16_t anz = model->rowCount();
            if (anz == 0)
            {
                ret.insert("anz", anz);
            }
            else
            {
                for (uint16_t i = 0; i < anz; ++i)
                {
                    QJsonObject temp {};
                    QSqlRecord tempRec {model->record(i)};
                    for (uint16_t ii = 0; ii < tempRec.count(); ++ii)
                    {
                        temp.insert(tempRec.fieldName(ii), tempRec.value(ii).toString());
                    }
                    array.insert(array.size(), temp);
                }
                ret.insert(tableNameArg, array);
            }
            qDebug() << ">> Database > searchData (" << __LINE__ << ") INFO " << ret;
    
            return ret;
        }
    

    The output:

    >> Database > searchData ( 716 ) INFO  QSqlDatabase(driver="QSQLITE", database="cfg.db", host="", port=-1, user="", open=true)
    >> Database > searchData ( 720 ) INFO tableNameArg= "config"
    >> Database > searchData ( 721 ) INFO whereArg= "(group='global') AND (key=config_set)"
    >> Database > searchData ( 723 ) INFO select= false
    >> Database > searchData ( 724 ) INFO lastError= QSqlError("1", "Unable to execute statement", "near \"group\": syntax error")
    

    Can you help me where the problem is?
    Thank you for your help.
    BR
    martin

    1 Reply Last reply
    0
    • I Offline
      I Offline
      IgKh
      wrote on 1 Feb 2025, 15:51 last edited by
      #3

      group is a reserved word in SQL, being part of the keyword GROUP BY. If you have a column named group, it needs to be escaped according to the rules of the database you are using. You can use QSqlDriver::escapeIdentifier for that.

      P 1 Reply Last reply 1 Feb 2025, 17:08
      3
      • C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 1 Feb 2025, 15:44 last edited by
        #2

        Why do you use a QSqlTableModel to retrieve rows from the db instead a simple QSqlQuery?

        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
        • I Offline
          I Offline
          IgKh
          wrote on 1 Feb 2025, 15:51 last edited by
          #3

          group is a reserved word in SQL, being part of the keyword GROUP BY. If you have a column named group, it needs to be escaped according to the rules of the database you are using. You can use QSqlDriver::escapeIdentifier for that.

          P 1 Reply Last reply 1 Feb 2025, 17:08
          3
          • I IgKh
            1 Feb 2025, 15:51

            group is a reserved word in SQL, being part of the keyword GROUP BY. If you have a column named group, it needs to be escaped according to the rules of the database you are using. You can use QSqlDriver::escapeIdentifier for that.

            P Offline
            P Offline
            Pl45m4
            wrote on 1 Feb 2025, 17:08 last edited by Pl45m4 2 Jan 2025, 17:14
            #4

            @IgKh said in QSqlTableModel setFilter => Unable to execute statement:

            group is a reserved word in SQL, being part of the keyword GROUP BY

            Fun story:
            Because of something similar the whole server infrastructure "exploded" where I was working around 10 years ago :)
            Wasn't my fault, I swear :))
            DHCP server had the list/table of clients (MAC, client IP, subnet IP, name, email etc) stored in an SQL table, where the subnet column was named "range" (to configure a range of IPs)... WAS ok, for early SQL versions as the system was set up by former apprentices....
            until our head made an upgrade on all servers, which also introduced new SQL releases to Linux, where range became a reserved name/keyword...
            Around 500 clients with no IP after their leases ran out... and nobody knew why :)
            Took two full days until we figured out what happened :'-)


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

            ~E. W. Dijkstra

            J 1 Reply Last reply 1 Feb 2025, 21:15
            2
            • M msauer751 has marked this topic as solved on 1 Feb 2025, 19:55
            • P Pl45m4
              1 Feb 2025, 17:08

              @IgKh said in QSqlTableModel setFilter => Unable to execute statement:

              group is a reserved word in SQL, being part of the keyword GROUP BY

              Fun story:
              Because of something similar the whole server infrastructure "exploded" where I was working around 10 years ago :)
              Wasn't my fault, I swear :))
              DHCP server had the list/table of clients (MAC, client IP, subnet IP, name, email etc) stored in an SQL table, where the subnet column was named "range" (to configure a range of IPs)... WAS ok, for early SQL versions as the system was set up by former apprentices....
              until our head made an upgrade on all servers, which also introduced new SQL releases to Linux, where range became a reserved name/keyword...
              Around 500 clients with no IP after their leases ran out... and nobody knew why :)
              Took two full days until we figured out what happened :'-)

              J Offline
              J Offline
              JonB
              wrote on 1 Feb 2025, 21:15 last edited by JonB 2 Jan 2025, 21:15
              #5

              @Pl45m4
              If you want to be safe all column names in SQL statements should be "escaped". I have seen various SQLs use [], "" or ``for this.

              P 1 Reply Last reply 2 Feb 2025, 00:51
              0
              • J JonB
                1 Feb 2025, 21:15

                @Pl45m4
                If you want to be safe all column names in SQL statements should be "escaped". I have seen various SQLs use [], "" or ``for this.

                P Offline
                P Offline
                Pl45m4
                wrote on 2 Feb 2025, 00:51 last edited by Pl45m4 2 Feb 2025, 00:52
                #6

                @JonB

                With this hint you are about 12 years late or so :D
                Btw: Actually in cases like these there is no one to blame... do we know what might become a reserved name for something in five years from now?! :-)
                So you can never make anything "future-safe" ;-)


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

                ~E. W. Dijkstra

                J 1 Reply Last reply 2 Feb 2025, 07:54
                0
                • P Pl45m4
                  2 Feb 2025, 00:51

                  @JonB

                  With this hint you are about 12 years late or so :D
                  Btw: Actually in cases like these there is no one to blame... do we know what might become a reserved name for something in five years from now?! :-)
                  So you can never make anything "future-safe" ;-)

                  J Offline
                  J Offline
                  JonB
                  wrote on 2 Feb 2025, 07:54 last edited by JonB 2 Feb 2025, 07:55
                  #7

                  @Pl45m4 said in QSqlTableModel setFilter => Unable to execute statement:

                  With this hint you are about 12 years late or so :D

                  ?

                  do we know what might become a reserved name for something in five years from now?! :-)

                  So you can never make anything "future-safe" ;-)

                  That is why I wrote what I did. You "escape" all column (and table) name references wherever they are and whatever name is being used. You don't just do it for those words which are reserved at present. That is what good auto-generated code does like I used for SQL Server and what I did in my method for inserting any column names into SQL statements I generated. If you don't, fair enough, but you are taking a chance against future changes. Just saying.

                  P 1 Reply Last reply 2 Feb 2025, 13:53
                  0
                  • J JonB
                    2 Feb 2025, 07:54

                    @Pl45m4 said in QSqlTableModel setFilter => Unable to execute statement:

                    With this hint you are about 12 years late or so :D

                    ?

                    do we know what might become a reserved name for something in five years from now?! :-)

                    So you can never make anything "future-safe" ;-)

                    That is why I wrote what I did. You "escape" all column (and table) name references wherever they are and whatever name is being used. You don't just do it for those words which are reserved at present. That is what good auto-generated code does like I used for SQL Server and what I did in my method for inserting any column names into SQL statements I generated. If you don't, fair enough, but you are taking a chance against future changes. Just saying.

                    P Offline
                    P Offline
                    Pl45m4
                    wrote on 2 Feb 2025, 13:53 last edited by
                    #8

                    @JonB said in QSqlTableModel setFilter => Unable to execute statement:

                    ?

                    Escaping the column names and doing everything more thoughtfully would have prevented the crash... so "range" instead of range etc...
                    I think not only any select statements were influenced but the whole table itself. That's why the DHCP service didn't start up anymore ;)


                    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

                    3/8

                    1 Feb 2025, 15:51

                    topic:navigator.unread, 5
                    • Login

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