BostonAreaHuman
BostonAreaHuman

Reputation: 1469

SQLalchemy issue recovering binary data from MSSQL Column

From the database

Id EndpointId  MacAddress  IsDefault   IsAuto

16 -2147483641 0x08002781A502 1 1 20 -2147483639 0x005056A54212 1 1

MacAddress column is BINARY(6) datatype

I'm using FreeTDS with Python and SQLAlchemy to get the data.

mymetadata = MetaData(engine_idf)

macadds = Table('EndpointsMacs',mymetadata,schema='dbo',autoload=True)

for row in macadds.select(macadds.c.IsDefault == 1).execute():
    print row

What I get back is.

(16, -2147483641, "\x08\x00'\x81\xa5\x02", True, True)

(20, -2147483639, '\x00PV\xa5B\x12', True, True)

I need to get the third value in the text equivalent so that I can work with the actual value of 08002781A502

Thanks

Upvotes: 2

Views: 145

Answers (1)

falsetru
falsetru

Reputation: 369474

You can use binascii.hexlify (or binascii.h2a_hex) to get hexadecimal representation:

>>> import binascii
>>> binascii.hexlify("\x08\x00'\x81\xa5\x02")
'08002781a502'

and binascii.unhexlify in case you want to get binary data representation:

Upvotes: 1

Related Questions