找回密码
 立即注册
收起左侧

数据库select

8
回复
7726
查看
[复制链接]
累计签到:4 天
连续签到:1 天
来源: 2013-12-10 15:13:12 显示全部楼层 |阅读模式
1Qter豆
  1. QSqlQuery query;
  2.             findID = 10;
  3.             query.exec("select id from fcframe1 where id = findID ");
复制代码
query.exec("select id from fcframe1 where id = findID ");  这句话中的findID好像不可以,必须是实实在在的数字,有没有什么办法可以用变量?

最佳答案

查看完整内容

你看在SQL中 怎么执行你照着搬过来就行了,其实说的很简单 select id from fcframe1 where id = 1 代码: int findID = 1; QString sql = "select id from fcframe1 where id = " + QString::fromNumber(findID);
回复

使用道具 举报

累计签到:410 天
连续签到:1 天
2013-12-10 15:13:13 显示全部楼层
pursue 发表于 2013-12-10 17:04
还有就是因为id 是unsigned int 类型,不是string类型,也必须 id = 'XXXXX'?

你看在SQL中 怎么执行你照着搬过来就行了,其实说的很简单
select id from fcframe1 where id = 1
代码:
int findID = 1;
QString sql = "select id from fcframe1 where id = " + QString::fromNumber(findID);
回复

使用道具 举报

累计签到:410 天
连续签到:1 天
2013-12-10 16:35:44 显示全部楼层
select id from fcframe1 where id = findID
看样子你的数据库不是很熟悉
select id from fcframe1 where id = 'xxxxx' 这样才能执行吧
findID 可以弄变量。
QString findID;
findID = "11111";
QString sql = "select id from fcframe1 where id = \'" + findID + "\'";
回复

使用道具 举报

累计签到:4 天
连续签到:1 天
2013-12-10 16:59:57 显示全部楼层
a408815041 发表于 2013-12-10 16:35
select id from fcframe1 where id = findID
看样子你的数据库不是很熟悉
select id from fcframe1 where i ...

\'" + findID + "\'";为何要有\"'+还有这个双引号有没有多了?
回复

使用道具 举报

累计签到:4 天
连续签到:1 天
2013-12-10 17:04:25 显示全部楼层
a408815041 发表于 2013-12-10 16:35
select id from fcframe1 where id = findID
看样子你的数据库不是很熟悉
select id from fcframe1 where i ...

还有就是因为id 是unsigned int 类型,不是string类型,也必须 id = 'XXXXX'?
回复

使用道具 举报

累计签到:76 天
连续签到:1 天
2013-12-10 18:04:56 显示全部楼层
query.exec(("select id from fcframe1 where id = %1 ").arg(findID))
回复

使用道具 举报

尚未签到

2013-12-11 11:32:16 显示全部楼层
query.prepare("select id from fcframe1 where id =: findID ");
query.bindValue(1, findID);
query.exec();
回复

使用道具 举报

累计签到:5 天
连续签到:1 天
2016-12-22 23:00:25 显示全部楼层
哈哈哈,对上面的总结一波,两种方法:
1.
  1. QSqlQuery query;
  2. int findID = 10;
  3. query.exec("select id from fcframe1 where id = " + QString::number(findID));
复制代码
2.
  1. QSqlQuery query;
  2. int findID = 10;
  3. query.prepare("select id from fcframe1 where id = :ID");
  4. query.bindValue(":ID", findID);
  5. query.exec();
复制代码
回复

使用道具 举报

累计签到:5 天
连续签到:1 天
2016-12-22 23:01:37 显示全部楼层
小弹弹 发表于 2016-12-22 23:00
哈哈哈,对上面的总结一波,两种方法:
1.2.

其实第一种方法就是将两个字符串连接起来
第二种方法是使用了名称绑定
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了