I have a code that prints the query from db, I have no problems displaying values for Varchar, int and other format, But the value of Byteary is different. The value on the DB is something like 0xA09080BD1160AB16 but the result in print of python is like
b'\x03\x80\x03\x8c\x00\x03\x00S^k\xdb' //not actual value
Based on what I have read on the net, there is a conn.add_output_converter(pyodbc.SQL_BINARY, hexToString) but its not working, the
pyodbc version is at 4.0.18
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 * from myDb.TestDb """
conn_str = (
r'Driver={ODBC Driver 13 for SQL Server};'
r'Server=yourserver\test;'
r'Database=test;'
r'Trusted_Connection=yes;'
r'CHARSET=UTF8;'
)
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()
b'\xa0\x90\x80\xbd\x11`\xab\x16'b'\xa0\x90...') instead of a string representation of that value ('0xA090...')?