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. I found a bug in the program in QString
Forum Updated to NodeBB v4.3 + New Features

I found a bug in the program in QString

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 321 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.
  • F Offline
    F Offline
    fanxinglanyu
    wrote on last edited by
    #1
        QString sql = QString("SELECT ComName FROM ComInfo WHERE ComCode LIKE \'%%1%\' LIMIT %2 OFFSET %3").arg("0107").arg(50).arg(0);
        qDebug()<<"sql:"<<sql;
    

    The above code returns the output:
    在这里插入图片描述

    The first '% 'and' 0107 'in' '%%1%' 'work so that the original '% 1' should have been replaced by 5007 with 0107 times, that is, the second replaced by 50 times the 01 part, and the second '% 2 'replaced the 0 which should have been the third one.

    2 Solutions
    Set 'LIMIT %2 OFFSET %3' as a Qstring and then splicing it together.
    Such as:

        QString sql = QString("SELECT ComName FROM ComInfo WHERE ComCode LIKE \'%%1%\'").arg("0107") + QString(" LIMIT %2 OFFSET %3").arg(50).arg(0);
    
    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      Ummm...you probably need to escape the (%) that you don't intend to substitute.

      I light my way forward with the fires of all the bridges I've burned behind me.

      1 Reply Last reply
      0
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        It's not a bug, it works as intended and documented.
        Don't build your query this way to avoid sql injection. Use prepared query and bind values.

        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
        7
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          @Christian-Ehrlicher said in I found a bug in the program in QString:

          Don't build your query this way to avoid sql injection.

          Can't stress enough how important this is.

          In other use cases, to avoid that "bug" you can use this version of arg:
          QStringLiteral("\'%%1%\' LIMIT %2 OFFSET %3").arg(QStringLiteral("0107"),QStringLiteral("50"),QStringLiteral("0"));

          "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

          1 Reply Last reply
          5

          • Login

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