SQL/JDBC 中的内联 BLOB/BINARY 数据类型
假设我想避免在 JDBC 中使用绑定变量并使用“临时”语句运行 SQL,例如:
connection.createStatement().executeQuery("SELECT ...");
是否有任何约定/JDBC 转义语法来内联 BLOB 数据类型?我知道 H2 有这种语法:
INSERT INTO lob_table VALUES (X'01FF');
但这不是标准。有通用的解决方案吗?请注意,我对通用方法感兴趣。我知道这可能会变得非常低效。
Let's say I want to avoid using bind variables in JDBC and run SQL using "ad-hoc" statements, e.g:
connection.createStatement().executeQuery("SELECT ...");
Is there any convention / JDBC escape syntax to inline BLOB data types? I know that H2 has this syntax:
INSERT INTO lob_table VALUES (X'01FF');
But that's not a standard. Any general solutions? Note, I'm interested in a general approach. I know that this can turn out to be terribly inefficient.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可能没有 JDBC 转义语法,因此我进行了一些搜索,发现并成功测试了以下内容:
SQL Server、Sybase ASE、Sybase SQL Anywhere
DB2
Derby、H2、HSQLDB、Ingres、MySQL、SQLite
Oracle
Postgres
有关更多详细信息,请参阅 AH 的回答 Postgres 的十六进制编码
SQL标准
There probably isn't a JDBC escape syntax, so I searched around a bit and found and successfully tested the following:
SQL Server, Sybase ASE, Sybase SQL Anywhere
DB2
Derby, H2, HSQLDB, Ingres, MySQL, SQLite
Oracle
Postgres
See A.H.'s answer for more details about Postgres' hex encoding
SQL Standard
我想在 Lukas 的回答中添加一些 PostgreSQL 特定的内容:
最短和最简单的解决方案是(因为 PostgreSQL至少 9.0):
没有任何转换(如果该列已经是
bytea
之一),并且在开头只有 一个\\x
标记。这是二进制数据类型。关于
X'01FF'
语法:根据 字符串常量 文档 PostgreSQL 确实支持它 - 对于位字符串。而且似乎没有从位到字节的标准转换。I'd like to add some PostgreSQL specific stuff to Lukas' answer:
The shortest and most easiest solution would be (since PostgreSQL 9.0 at least):
without any cast (if the column is already a
bytea
one) and only one\\x
mark right at the beginning. This is the "hex format" documented in the section Binary Data Types.Regarding the
X'01FF'
syntax: According to the string constant documentation PostgreSQL does support it - for bit strings. And it seems, that there is no standard conversion from bit to bytea.CONCAT_RAWS
和RAWS
的来源在 Oracle 10:使用 HEXTORAW 填充 blob 数据。The source for
CONCAT_RAWS
andRAWS
is given in Oracle 10: Using HEXTORAW to fill in blob data.