【发布时间】:2016-02-18 05:43:35
【问题描述】:
假设我有以下 SQLite 表定义:
create table test (id integer primary key, info integer);
以及以下条目:
id | info
----------
1 | 10
2 | 20
3 | 30
我想使用 Qt 的 QSqlQuery 类来 prepare() 查询并使用 bindValue() 函数。
我想要达到的目标是
insert into test values (
( select id from test where ROWID = last_insert_rowid() )+100,
666
);
为了得到:
id | info
----------
1 | 10
2 | 20
3 | 30
103 | 666
虽然通过QSqlQuery qry 对象直接exec()ing 语句起作用,但这个
//qry is set up correctly.
qry.prepare("insert into test values (?,?);");
qry.bindValue(0, "select id from test where ROWID = last_insert_rowid() )+100");
qry.bindValue(1,666);
qry.exec();
不起作用(数据类型不匹配)。
1) 如何使用bindValue() 使其工作?
2) 在不使用last_insert_rowid() 的情况下实现相同行为的最简洁方法是什么?
3) 如果表到目前为止没有行,上面的代码将为id 返回什么值?零?
【问题讨论】:
标签: c++ qt sqlite qsqlquery bindvalue