Qsql查询检查qt中是否存在整数 [英] Qsql query to check whether integer is present or not in qt

查看:281
本文介绍了Qsql查询检查qt中是否存在整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

num是一个整数,PassID也是一个整数

qry.prepare(SELECT * FROM ISSUEPASS WHERE PassID = num);

我需要选择行PassID匹配的地方

我收到错误无法获取行没有查询



我尝试过:



qry.prepare(SELECT * FROM ISSUEPASS WHERE PassID = num);

解决方案

您必须创建将 num 作为参数传递给字符串格式化函数的查询字符串。因为 QSqlQuery :: prepare()| Qt SQL 5.10 [ ^ ]期望一个 QString 参数,它可以在一个语句中完成:

 qry.prepare(QString (  SELECT * FROM ISSUEPASS WHERE PassID =%1)。arg(num)); 



但是SQL命令绝不应该使用创建的文本字符串来避免SQL注入(这里没有文本参数也没关系)。首选解决方案是使用参数化查询:

 qry.prepare(  SELECT * FROM ISSUEPASS WHERE PassID =:passId); 
qry.bindValue( :passId,num);


num is a integer and PassID also a integer
qry.prepare("SELECT * FROM ISSUEPASS WHERE PassID =num");
I need to select the row where the PassID matches
Iam getting error "unable to fetch row No query"

What I have tried:

qry.prepare("SELECT * FROM ISSUEPASS WHERE PassID =num");

解决方案

You have to create the query string passing num as parameter to a string formatting function. Because QSqlQuery::prepare() | Qt SQL 5.10[^] expects a QString argument, it can be be done in a single statement:

qry.prepare(QString("SELECT * FROM ISSUEPASS WHERE PassID =%1").arg(num));


But SQL commands should never use created text strings to avoid SQL injections (here it would be OK because there is no text parameter). The preferred solution is using parametrised queries:

qry.prepare("SELECT * FROM ISSUEPASS WHERE PassID =:passId");
qry.bindValue(":passId", num);


这篇关于Qsql查询检查qt中是否存在整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆