Qt QSqlQuery prepare and bindValue not working

雨燕双飞 提交于 2020-01-02 08:15:11

问题


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 :/


回答1:


instead of

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

your prepare statement should read

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



来源:https://stackoverflow.com/questions/13111130/qt-qsqlquery-prepare-and-bindvalue-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!