Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. The QSqlQuery returns exceptions for the words starting with some digits
Qt 6.12 LTS is out! See what's new in the release blog

The QSqlQuery returns exceptions for the words starting with some digits

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 3.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.
  • lmofallisL Offline
    lmofallisL Offline
    lmofallis
    wrote on last edited by
    #1

    I have this code:

    foreach (QString TheKeyword, KeywordsList) {
        if (!TheKeyword.isEmpty())
        {
            TheKeyword.replace("'","''").replace("[", "[[]");
    
            QString sQuery = QString("UPDATE news SET deleted = '1' WHERE %1 like '%%2%' AND Id IN(%3) AND deleted = '0'")
                                .arg(TheColumn)
                                .arg(TheKeyword)
                                .arg(TheIDs);
    
            QSqlQuery TheQuery(TheDB);
            TheQuery.prepare(sQuery);
            TheQuery.exec();
    
            if (TheQuery.lastError().isValid())
            {
                qDebug() << TheQuery.lastError() << "   -   " << TheKeyword;
            }
        }
    }
    

    For example, the "KeywordsList" contains this word's list (The "KeywordsList" is a QStringList):

    1Tap
    World
    A man from Mars
    2 Battery
    2Do
    1CLICK
    01 Net
    2 Bananas
    4 Bananas
    system app remover
    

    My code returns this errors:

    QSqlError("", "Unable to fetch row", "No query")    -    "1Tap"
    QSqlError("", "Unable to fetch row", "No query")    -    "2 Battery"
    QSqlError("", "Unable to fetch row", "No query")    -    "2Do"
    QSqlError("", "Unable to fetch row", "No query")    -    "1CLICK"
    QSqlError("", "Unable to fetch row", "No query")    -    "01 Net"
    QSqlError("", "Unable to fetch row", "No query")    -    "2 Bananas"
    

    So, I see that problem is only with the keywords starting with '0', '1' or '2'

    I tried to use bindValue, but without success.

    Thank you.

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

      Hi,

      You should print the query run with QSqlQuery::lastQuery. That might give you additional clues.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      lmofallisL 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        You should print the query run with QSqlQuery::lastQuery. That might give you additional clues.

        lmofallisL Offline
        lmofallisL Offline
        lmofallis
        wrote on last edited by lmofallis
        #3
        "UPDATE news SET deleted = '1' WHERE title like '146,293,294,295,297,193,219,231,291Tap%' AND Id IN(%3) AND deleted = '0'"
        "UPDATE news SET deleted = '1' WHERE title like '146,293,294,295,297,193,219,231,291 Battery%' AND Id IN(%3) AND deleted = '0'"
        "UPDATE news SET deleted = '1' WHERE title like '146,293,294,295,297,193,219,231,291Do%' AND Id IN(%3) AND deleted = '0'"
        "UPDATE news SET deleted = '1' WHERE title like '17,208,224,228,60,230,146,293,294,295,297,193,219,231,291CLICK%' AND Id IN(%3) AND deleted = '0'"
        "UPDATE news SET deleted = '1' WHERE title like '168,59,67,229,180,204,205,206 Net%' AND Id IN(%3) AND deleted = '0'"    -    "01 Net"
        "UPDATE news SET deleted = '1' WHERE title like '9,10,11,12,13,14,15,16,198,199,200,168,195,196,197 Bananas%' AND Id IN(%3) AND deleted = '0'"
        
        1 Reply Last reply
        0
        • lmofallisL Offline
          lmofallisL Offline
          lmofallis
          wrote on last edited by lmofallis
          #4

          For example I think that the thing is dealt as:

          %%2%
          becomes
          %2 Battery
          then
          146,293,294,295,297,193,219,231,291 Battery%

          jsulmJ 1 Reply Last reply
          0
          • lmofallisL lmofallis

            For example I think that the thing is dealt as:

            %%2%
            becomes
            %2 Battery
            then
            146,293,294,295,297,193,219,231,291 Battery%

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

            @lmofallis You should use bindValue (see http://doc.qt.io/qt-5/qsqlquery.html) instead of using % and arg from QString.

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

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              I don't think you can use bindValue in these cases (a column name, a like block and an in block)

              On the other hand, a few things going wrong:

              • TheKeyword.replace("'","''").replace("[", "[[]"); do not escape manually, use TheDB->driver()->escapeIdentifier(TheKeyword,QSqlDriver::FieldName);
              • #include <QStringBuilder> (for efficiency) and build the the query (no need to use arg() here as this is not a user visible string and does not need internationalisation):
              const QString sQuery = "UPDATE news SET deleted = '1' WHERE " 
              % TheDB->driver()->escapeIdentifier(TheColumn,QSqlDriver::FieldName) 
              % " like '%" % TheDB->driver()->escapeIdentifier(TheKeyword,QSqlDriver::FieldName) 
              % "%' AND Id IN(" % TheDB->driver()->escapeIdentifier(TheIDs,QSqlDriver::FieldName) % ") AND deleted = '0'";
              
              • add an if around exec to make sure the query was executed

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              lmofallisL 1 Reply Last reply
              1
              • VRoninV VRonin

                I don't think you can use bindValue in these cases (a column name, a like block and an in block)

                On the other hand, a few things going wrong:

                • TheKeyword.replace("'","''").replace("[", "[[]"); do not escape manually, use TheDB->driver()->escapeIdentifier(TheKeyword,QSqlDriver::FieldName);
                • #include <QStringBuilder> (for efficiency) and build the the query (no need to use arg() here as this is not a user visible string and does not need internationalisation):
                const QString sQuery = "UPDATE news SET deleted = '1' WHERE " 
                % TheDB->driver()->escapeIdentifier(TheColumn,QSqlDriver::FieldName) 
                % " like '%" % TheDB->driver()->escapeIdentifier(TheKeyword,QSqlDriver::FieldName) 
                % "%' AND Id IN(" % TheDB->driver()->escapeIdentifier(TheIDs,QSqlDriver::FieldName) % ") AND deleted = '0'";
                
                • add an if around exec to make sure the query was executed
                lmofallisL Offline
                lmofallisL Offline
                lmofallis
                wrote on last edited by
                #7

                @VRonin
                Thank you.
                Your escaping way works very well. I'll check my old projects to update them.

                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
                • Unsolved