在客户端转换为十六进制与使用 rawtohex 有什么区别?

发布于 2024-07-25 02:20:00 字数 780 浏览 5 评论 0原文

我有一个像这样创建的表:

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 技术交流群。

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

发布评论

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

评论(3

拒绝两难 2024-08-01 02:20:00

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.

静谧幽蓝 2024-08-01 02:20:00

Oracle 中的 RAWTHEX 对位顺序不敏感,而在您的计算机上它当然是敏感的。

另请注意,RAWTOHEX() 的参数可以由您的库隐式转换为 VARCHAR2(即作为 SQLT_STR 传输),这使得它也编码和排序规则敏感。

RAWTOHEX in Oracle is bit order insensitive, while on your machine it's of course sensitive.

Also note that an argument to RAWTOHEX() can be implicitly converted to VARCHAR2 by your library (i. e. transmitted as SQLT_STR), which makes it also encoding and collation sensitive.

走野 2024-08-01 02:20:00

我通常会设置正确的变量绑定类型,特别是在尝试将 Oracle 的 RAW 数据类型传递到查询中时。

例如:

self.connection.setinputsizes(cx_Oracle.BINARY)
self.connection.execute(
    "INSERT INTO bin_test (b) VALUES (rawtohex(?))",
    (value,)
)

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:

self.connection.setinputsizes(cx_Oracle.BINARY)
self.connection.execute(
    "INSERT INTO bin_test (b) VALUES (rawtohex(?))",
    (value,)
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文