我试图使用Elixir Ecto.Adapters.SQL.query插入postgres,查询由字符串组成的标记数组组成。不过,它确实嵌入到postgres中,但不是作为字符串列表,而是作为单个字符串。也就是说,如果标签包含"abc“、"xyz",它将在postgres中插入abcdef而不是数组。
值得一提的是,我不使用模式或迁移。简单的原始查询。
需要插入postgres的地图
response_map= %{"description" => "des33", "id" => 158,
"inserted_at" => "2017-06-09T10:19:03.904572", "tags" => ["abc","xyz"],
"title" => "test 1", "updated_at" => "2017-06-09T10:19:03.904578"}我使用的代码
%{"description" => desc, "id" => id, "title" => title, "tags" => tags}=response_map
Ecto.Adapters.SQL.query!(Repo,"insert into test values ('#{id}','#{title}','#{desc}','#{tags}')") 发布于 2017-06-09 10:27:15
您不应该在查询中使用字符串内插。,您正在打开自己的SQL注入攻击。使用参数化查询,您将修复SQL注入,并使其与数组一起工作:
Ecto.Adapters.SQL.query!(Repo, "insert into test values ($1, $2, $3, $4)", [id, title, desc, tags]) https://stackoverflow.com/questions/44455290
复制相似问题