我想知道如何将多个参数传递给 sqllite 中的原始查询。
我的代码在下面
query() async {
// get a reference to the database
Database db = await DatabaseHelper.instance.database;
// raw query
List<Map> result = await db.rawQuery('SELECT * FROM my_table WHERE name=?', ['Peter']);
// print the results
result.forEach((row) => print(row));
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我传递了一个参数“Peter”,但是如果我想传递多个参数,例如:
List<Map> result = await db.rawQuery('SELECT * FROM my_table WHERE name=? and last_name=? and year=?', ['Peter'], ['Smith'],[2019]);
Run Code Online (Sandbox Code Playgroud)
如果我执行上面的代码,我会收到错误“位置参数太多:预期为 2,但找到了 4”。有人可以告诉我如何将多个参数传递给 sqllite flutter 中的查询吗?
liv*_*ove 12
或者如果你想使用查询功能,你可以这样做:
String strVal = 'str';
int intVal = 0;
String likeVal = 'str';
final List<Map<String, dynamic>> maps = await db.query('my_table',
where: "col1 LIKE ? and col2 = ? and col3 = ?",
whereArgs: ['$likeVal%', strVal, intVal],
orderBy: 'id',
limit: 10);
Run Code Online (Sandbox Code Playgroud)
我假设您正在使用sqflite。
您需要将所有参数放在一个列表中,而不是多个。下面的代码应该像这样工作:
List<Map> result = await db.rawQuery(
'SELECT * FROM my_table WHERE name=? and last_name=? and year=?',
['Peter', 'Smith', 2019]
);
Run Code Online (Sandbox Code Playgroud)
有关如何使用原始查询的更多示例,请参阅其pub.dev 页面上的示例。
| 归档时间: |
|
| 查看次数: |
3251 次 |
| 最近记录: |