将映射值插入字符串
inserting value of map to string
我看了一下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 '{'.
。我也尝试了一些缩写,但没有任何帮助。
那么问题来了。如何将映射的值正确插入到字符串中?
来自 PostgreSQL 驱动程序的示例代码意味着 query()
方法需要两个参数,一个字符串和一个映射。
你的第二个例子似乎试图进行字符串插值。在 Dart 中,这看起来像
var id = 5;
var string = 'select color from crayons where id = $id'; // or ${id}
print(string);
请不要在 SQL 时尝试此操作。这为 SQL 注入附件打开了一个大洞。
PostgresSQL 以安全的方式进行自己的字符串插值。
我看了一下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 '{'.
。我也尝试了一些缩写,但没有任何帮助。
那么问题来了。如何将映射的值正确插入到字符串中?
来自 PostgreSQL 驱动程序的示例代码意味着 query()
方法需要两个参数,一个字符串和一个映射。
你的第二个例子似乎试图进行字符串插值。在 Dart 中,这看起来像
var id = 5;
var string = 'select color from crayons where id = $id'; // or ${id}
print(string);
请不要在 SQL 时尝试此操作。这为 SQL 注入附件打开了一个大洞。
PostgresSQL 以安全的方式进行自己的字符串插值。