Cli*_*Dog 3 python postgresql psycopg2 bytea
我有一个小脚本,我正在生成SHA1哈希,通过hashlib的hexdigest获取二进制表示,并将它们存储在带有bytea列的Postgres DB中.我在Postgres(缩写)中有一个看起来像这样的查询:
SELECT * FROM some_table WHERE some_hash in decode(another_hash, 'hex')
Run Code Online (Sandbox Code Playgroud)
执行查询时,我有一些代码如下:
cur.execute(query)
for hash_rep in cur:
print bhash
Run Code Online (Sandbox Code Playgroud)
现在,在该print语句中,它将打印出难以理解的字符,或者如果我将其更改为:
print str(psycopg2.Binary(bhash))
Run Code Online (Sandbox Code Playgroud)
我有类似的东西:
'\214p\203\030\017K\247\373o\253'::bytea
Run Code Online (Sandbox Code Playgroud)
将它转换回原始字符串的正确方法是什么?原始表示类似于"30d22d5d64efe4c5333e",我想将其恢复到原始字符串以进行比较.我不确定我是否遗漏了一些明显的东西,
既然你要求Postgres解码你的十六进制字符串并将其存储为二进制,你应该让postgres在输出时将其编码回十六进制.
SELECT encode(some_hash, 'hex'), * FROM some_table WHERE some_hash in decode(another_hash, 'hex')
Run Code Online (Sandbox Code Playgroud)
或者,您可以在python中进行编码.试试binascii.hexlify(数据).
| 归档时间: |
|
| 查看次数: |
11296 次 |
| 最近记录: |