mysql - 动态表达式 Groovy SQL 的引用

标签 mysql postgresql groovy

我正在使用 Groovy 对数据库进行查询,查询工作正常并返回正确的数据,但是我在终端中收到此错误。

In Groovy SQL please do not use quotes around dynamic expressions (which start with $) as this means we cannot use a JDBC PreparedStatement and so is a security hole. Groovy has worked around your mistake but the security hole is still there.

这是我的查询

sql.firstRow("""select elem
                        from site_content,
                        lateral jsonb_array_elements(content->'playersContainer'->'series') elem
                        where elem @> '{"id": "${id}"}'
                     """)

如果我将其更改为 $id 或

sql.firstRow("""select elem
                        from site_content,
                        lateral jsonb_array_elements(content->'playersContainer'->'series') elem
                        where elem @> '{"id": ?}'
                     """, id)

我收到以下错误

org.postgresql.util.PSQLException: The column index is out of range: 1, number of columns: 0.

最佳答案

位置命名参数由groovy sql正确处理,应该使用而不是“'$id'”

正如@Opal提到的和描述的here ,您应该将参数作为 listmap 传递:

sql.execute "select * from tbl where a=? and b=?", [ 'aa', 'bb' ]
sql.execute "select * from tbl where a=:first and b=:last", first: 'aa', last: 'bb'

关于mysql - 动态表达式 Groovy SQL 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41719911/

相关文章:

python - 从 python 脚本启动 MySQL

php - 如何在php中将mysql数据库中的数据存储为关联数组

postgresql - 在子查询中选择数组的 Postgres ANY 运算符

postgresql - IBM容器卷的性能

spring-boot - RestfulController POST(save)方法中动态字段的映射

string - Groovy:从字符串计算的时间减法

python - 使用 _mysql 修复 SQL 注入(inject)

mysql查询获取具体记录

postgresql - 监视 Postgres 表事件

arrays - 如何用groovy语言将项目一一添加到数组中