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. SQL querry with "LIKE" not working
Qt 6.12 LTS is out! See what's new in the release blog

SQL querry with "LIKE" not working

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.2k 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.
  • E Offline
    E Offline
    Eidus
    wrote on last edited by
    #1

    Hi,
    I don't understand, why this SQL querry is not working with "LIKE".
    I need to find all the entries in the database, starting with symbols for example "DA". This requires adding a "%" symbol - "DA%".
    If I run this querry (below) in SQLite Manager (Mozilla), it's finding 1505 items. In my QT app he finds only 257 items. ??
    And when i give this part of the code (irei+'\u0025') to querry search->prepare("SELECT * FROM zod WHERE pav LIKE ''"+iz+"'+'\u0025''") - he does not find any record.
    Please, help - where do I make a mistake?
    Thank you in advance
    ................................................
    void MainWindow::on_pushButtonSearch_clicked()
    {
    QString irei;
    QString iz;
    irei = ui->lineEditPaieska->text();
    iz=irei+'\u0025';
    db.open();
    QSqlQueryModel *mod = new QSqlQueryModel();
    QSqlQuery *search = new QSqlQuery(db);
    search->prepare("SELECT * FROM zod WHERE pav LIKE '"+iz+"'");
    search->exec();
    mod->setQuery(*search);
    ui->tableView->setModel(mod);
    qDebug()<<(mod->rowCount());
    db.close();
    }

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

      Hi and welcome to devnet,

      IIRC, when building such a string you need to double the % sign. This sign is used for positional arguments when building a QString e.g. QString myString = QString("%1 pieces").arg(numberOfPieces);.

      On a side note, why are you creating a new QSqlQueryModel each time you call that method ?
      There's also no need to allocate the QSqlQuery object on the heap, just use the stack.
      Why are you opening and closing the database in the same method ?
      On another note, you shouldn't store QSqlDatabase objects as class members. It's explained in the QSqlDatabase documentation details.

      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
      1
      • E Offline
        E Offline
        Eidus
        wrote on last edited by
        #3

        Understood, fixed it, works !!!
        Thank you very much.

        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