toan
toan

Reputation: 11

Read uniqueidentifier field from MSSQL using python

I used pyodbc to access my MSSQL database.

When reading uniqueidentifier field from MSSQL, in my MacOS, I can print the correct value of udid field (e.g 4C444660-6003-13CE-CBD5-8478B3C9C984), however when I run the same code on Linux CentOS, i just see very strange string like "???E??6??????c", and the type of value is "buffer", not "str" as in MacOS.

Could you explain me why it is and how can i get correct value of uidi on linux? Thanks

Upvotes: 0

Views: 2664

Answers (2)

FlipperPA
FlipperPA

Reputation: 14351

This is a few years old, but I've had to tackle this same problem recently. My solution was to simply CAST the unique identifier as a VARCHAR, which kept my Python code nice and tidy:

SELECT CAST(unique_id_column AS VARCHAR(36)) AS my_id FROM...

Then in Python, simply output row.my_id.

Upvotes: 1

toan
toan

Reputation: 51

In linux i use str(uuid.UUID(bytes_le=value)).upper() to get string like 4C444660-6003-13CE-CBD5-8478B3C9C984 of uniqueidentifier field

Upvotes: 5

Related Questions