Solved SQL querry with "LIKE" not working
-
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();
} -
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. -
Understood, fixed it, works !!!
Thank you very much.