Qt QSqlQuery prepare and bindValue not working

2019-05-23 02:45发布

I have a problem with prepare and bindValue :(

db.open();
QSqlQuery q;
    q.prepare("SELECT id_malade,nom,prenom FROM Malade WHERE nom LIKE %:p% OR prenom = %:f% ;");
    q.bindValue(":p",ui->lineEdit->text());
    q.bindValue(":f",ui->lineEdit->text());
    qDebug() << q.boundValue(0) << " " << q.boundValue(1);
    qDebug() << q.executedQuery().toStdString().c_str(); db.close();

output is:

QVariant(QString, "zit")   QVariant(QString, "zit") 
SELECT id_malade,nom,prenom FROM Malade WHERE nom LIKE %?% OR prenom = %?% ;

I tried to change :p and :f with ? and use int positions in bindValue but no luck. The query got executed with success so I couldn't fetch the exact error. I used prepare and bindValue a lot in my program and it works fine the problem is only on this class :/

标签: c++ qt prepare
1条回答
放荡不羁爱自由
2楼-- · 2019-05-23 03:34

instead of

[...] nom LIKE %:p% OR prenom = %:f%

your prepare statement should read

[...] nom LIKE '%'||:p||'%' OR prenom = '%'||:f||'%'

查看更多
登录 后发表回答