为什么使用准备好的语句会因为空值而失败,而使用 GString 则会成功?

发布于 2024-09-24 13:44:53 字数 1539 浏览 6 评论 0原文

简而言之,问题是:

当尝试使用准备好的语句和 groovy.sql.Sql 插入具有一些空列的行时会发生这种情况:

groovy:000> val
===> [123123123, 2, null, 0, 0, , null, , 1213020112511801, 1283425009158952, 1, 2, null, 0, 0, , null, , 1213020112511801, 1283425009158952]
groovy:000> destSql.execute "insert into my_table values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", val
ERROR java.sql.SQLException: Type is not supported.
        at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException (Unknown Source)
        at org.apache.derby.client.am.SqlException.getSQLException (Unknown Source)
        at org.apache.derby.client.am.PreparedStatement.setObject (Unknown Source)
        at groovysh_evaluate.run (groovysh_evaluate:3)
        ...
groovy:000> 

我使用 destSql.dataSet("my_table") 得到了类似的结果。添加 valueMap

这就是尝试插入与 GString 相同的行时发生的情况:

groovy:000> destSql.execute "insert into my_table values (${val[0]}, ${val[1]}, ${val[2]}, ${val[3]}, ${val[4]}, ${val[5]}, ${val[6]}, ${val[7]}, ${val[8]}, ${val[9]})"
===> false
groovy:000> 

现在,我了解到使用准备好的语句插入空值存在问题(如文档所述,例如 此处此处)。

我想了解的是:

1)我很难相信用准备好的语句插入空值是不可能的,那么为什么它有时会导致问题呢?不够完善的 JDBC 驱动程序、数据库引擎?

2) 为什么使用 GString 的示例可以工作?遵循相同的逻辑,它不是仍然键入有关空列的信息吗?那么它是如何工作的呢?

The problem in a nutshell:

This is what happens when trying to insert a row with a few null columns using a prepared statement and groovy.sql.Sql:

groovy:000> val
===> [123123123, 2, null, 0, 0, , null, , 1213020112511801, 1283425009158952, 1, 2, null, 0, 0, , null, , 1213020112511801, 1283425009158952]
groovy:000> destSql.execute "insert into my_table values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", val
ERROR java.sql.SQLException: Type is not supported.
        at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException (Unknown Source)
        at org.apache.derby.client.am.SqlException.getSQLException (Unknown Source)
        at org.apache.derby.client.am.PreparedStatement.setObject (Unknown Source)
        at groovysh_evaluate.run (groovysh_evaluate:3)
        ...
groovy:000> 

I get a similar result with destSql.dataSet("my_table").add valueMap.

This is what happens when trying to insert the same row as a GString:

groovy:000> destSql.execute "insert into my_table values (${val[0]}, ${val[1]}, ${val[2]}, ${val[3]}, ${val[4]}, ${val[5]}, ${val[6]}, ${val[7]}, ${val[8]}, ${val[9]})"
===> false
groovy:000> 

Now, I understand that there are problems inserting null values using a prepared statement (as documented e.g. here and here).

What I would like to understand is the following:

1) I find it hard to believe that inserting nulls with prepared statements is impossible, so why does it sometimes cause problems? Less than perfect JDBC drivers, database engines?

2) Why does the example with the GString work? Following the same logic, doesn't it still type information even about the null columns, so how come it works?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み青杉依旧 2024-10-01 13:44:53

如果包含您遇到的实际异常,您将获得更好的响应。您可以在 Derby 服务器的 derby.log 文件中找到异常,也可以修改应用程序以打印整个异常链,而不仅仅是客户端最外层异常: http://wiki.apache.org/db-derby/UnwindExceptionChain

没有看到实际的异常,很难帮助你,但我会大胆猜测您正在看到 https://issues.apache.org/jira/browse /德比-1938

You'll get better responses if you include the actual exception that you've encountered. You can find the exception in your Derby server's derby.log file, or you can also modify your application to print the entire exception chain, not just the client-side outermost exception: http://wiki.apache.org/db-derby/UnwindExceptionChain

Without seeing the actual exception, it's quite hard to help you, but I'll venture a guess that you're seeing https://issues.apache.org/jira/browse/DERBY-1938

百善笑为先 2024-10-01 13:44:53

gstring 示例之所以有效,是因为如果数组元素为 null,代码 ${val[0]} 将返回 null

如果您在问题中插入的代码正确,则某些元素没有 null 值...这只是一个猜测

the gstring example is working because the code ${val[0]} will return null if the array element is null.

if the code you inserted in the question is correct, some of you elements do not have a null value... this just a guess

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文