在客户端转换为十六进制与使用 rawtohex 有什么区别?
我有一个像这样创建的表:
CREATE TABLE bin_test
(id INTEGER PRIMARY KEY, b BLOB)
使用Python和cx_Oracle,如果我这样做:
value = "\xff\x00\xff\x00" #The string represented in hex by ff00ff00
self.connection.execute("INSERT INTO bin_test (b) VALUES (rawtohex(?))",
(value,))
self.connection.execute("SELECT b FROM bin_test")
我最终会得到一个十六进制值a000a000
,这是不正确的! 但是,如果我这样做:
import binascii
value = "\xff\x00\xff\x00"
self.connection.execute("INSERT INTO bin_test (b) VALUES (?)",
(binascii.hexlify(value,)))
self.connection.execute("SELECT b FROM bin_test")
我会得到正确的结果。 我这里有一个类型转换系统,但是在这里描述它有点困难。 因此,有人可以指出我是否在 SQL 级别做错了什么或者我的转换是否发生了奇怪的事情吗?
I have a table that's created like this:
CREATE TABLE bin_test
(id INTEGER PRIMARY KEY, b BLOB)
Using Python and cx_Oracle, if I do this:
value = "\xff\x00\xff\x00" #The string represented in hex by ff00ff00
self.connection.execute("INSERT INTO bin_test (b) VALUES (rawtohex(?))",
(value,))
self.connection.execute("SELECT b FROM bin_test")
I eventually end up with a hex value of a000a000
, which isn't correct! However if I do this:
import binascii
value = "\xff\x00\xff\x00"
self.connection.execute("INSERT INTO bin_test (b) VALUES (?)",
(binascii.hexlify(value,)))
self.connection.execute("SELECT b FROM bin_test")
I get the correct result. I have a type conversion system here, but it's a bit difficult to describe it here. Thus, can someone point me in the right direction as to whether I'm doing something wrong at the SQL level or whether something weird is happening with my conversions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
rawtohex() 用于将 Oracle RAW 数据类型转换为十六进制字符串。 即使字符串包含二进制数据,您也可能会因为向其传递字符串而感到困惑。 在本例中,由于 Oracle 需要一个十六进制字符的字符串,因此请为其提供一个十六进制字符的字符串。
rawtohex() is for converting Oracles RAW datatypes to hex strings. It's possible it gets confused by you passing it a string, even if the string contains binary data. In this case, since Oracle expects a string of hex characters, give it a string of hex characters.
Oracle
中的RAWTHEX
对位顺序不敏感,而在您的计算机上它当然是敏感的。另请注意,
RAWTOHEX()
的参数可以由您的库隐式转换为VARCHAR2
(即作为SQLT_STR
传输),这使得它也编码和排序规则敏感。RAWTOHEX
inOracle
is bit order insensitive, while on your machine it's of course sensitive.Also note that an argument to
RAWTOHEX()
can be implicitly converted toVARCHAR2
by your library (i. e. transmitted asSQLT_STR
), which makes it also encoding and collation sensitive.我通常会设置正确的变量绑定类型,特别是在尝试将 Oracle 的 RAW 数据类型传递到查询中时。
例如:
I usually set the proper type of variable bindings specially when trying to pass an Oracle kinda of RAW data type into a query.
for example something like: