我看了一下postgresql driver dokumentation .我在那里找到了以下代码。
conn.query('select color from crayons where id = @id', {'id': 5})
.toList()
.then((result) { print(result); });
conn.execute('insert into crayons values (@id, @color)',
{'id': 1, 'color': 'pink'})
.then((_) { print('done.'); });
我想测试将id插入字符串并将以下代码放入try.dart.org
// Go ahead and modify this example.
void main() {
var string = 'select color from crayons where id = @id', {'id': 5};
print(string);
}
不幸的是,这给了我以下错误
Compilation failed: Expected identifier, but got '{'.
.我也尝试了一些缩写,但没有任何帮助。所以问题是。如何正确地将 map 的值插入字符串?
最佳答案
您来自 PostgreSQL 驱动程序的示例代码意味着 query()
方法需要两个参数,一个字符串和一个映射。
您的第二个示例似乎尝试进行字符串插值。在 Dart 中,这看起来像
var id = 5;
var string = 'select color from crayons where id = $id'; // or ${id}
print(string);
请不要为 SQL 尝试此操作。这为 SQL 注入(inject)附加打开了一个大漏洞。
PostgresSQL 以安全的方式进行自己的字符串插值。
关于dart - 将 map 的值插入字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30056975/