dart - 将 map 的值插入字符串

标签 dart

我看了一下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/

相关文章:

unit-testing - 单元测试 Dart 异步流

android - 获取用户SIM卡列表并选中-flutter

flutter - 在 flutter 中解码utf8字符串

android-studio - 在 flutter 应用程序中集成 srtipe 时出现 Gradle 异常

dart - 如何将函数限制为某些参数?

json - 在 flutter cast<RK, RV>() => Map<RK, RV> 中解析 JSON,没有这样的方法错误

linux - 异常 : No Linux desktop project configured error on flutter project

flutter - Flutter:将TextFormField onchange值更改为Text小部件

android-studio - 这个常量表达式的计算抛出一个表达式

macos - Mac OS Big sur Flutter安装错误 “Bad state: Future already completed”