[英]Python Pyodbc Binary Column is returning \xda\x08\xcd\x08\xba\x08 instead of numbers
I have a SQL database that displays a varbinary (max) like this 0x9406920691068F... I want to import it to python pycharm to get the same exact type of data. 我有一个显示这样的varbinary(max)的SQL数据库,例如0x9406920691068F ...我想将其导入到python pycharm中以获取相同的确切数据类型。 However, it shows something like this instead [b'\\x94\\x06\\x92\\x06\\x91\\x06\\x8f\\x06\\x8d.. how do I copy the same numbers to python? 但是,它显示的是类似的内容[b'\\ x94 \\ x06 \\ x92 \\ x06 \\ x91 \\ x06 \\ x8f \\ x06 \\ x8d ..如何将相同的数字复制到python? I am a beginner in python, please help. 我是python的初学者,请帮忙。
I copied the code from previous post and it didn't work 我复制了上一篇文章中的代码,但没有用
import pyodbc
def hexToString(binaryString):
try:
hashString = ["{0:0>2}".format(hex(b)[2:].upper()) for b in binaryString]
return '0x' + "".join(hashString)
except:
return binaryString
query = """ select P from Access.table """
conn_str = (
**** private database details # I don't copy on the page
)
cnxn = pyodbc.connect(conn_str)
cnxn.add_output_converter(pyodbc.SQL_BINARY, hexToString)
cursor = cnxn.cursor()
try:
cursor.execute(query)
row = cursor.fetchone()
except MySQLdb.error as err:
print(err)
else:
while row is not None:
print(row)
row = cursor.fetchone()
如果列返回类型为varbinary(max)
则需要添加输出转换器函数来处理SQL_VARBINARY
,而不是SQL_BINARY
cnxn.add_output_converter(pyodbc.SQL_VARBINARY, converter_function_name)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.