I have a table in SQL Server with a varbinary(max) column that contains file blobs. I am exporting these with Python and pyodbc like this:
import pyodbc
conn = pyodbc.connect('DSN=SQL Server;UID=username;PWD=password')
cursor = conn.cursor()
with open("output.pdf", "wb") as output_file:
cursor.execute("SELECT top 1 filedata from schm.table_name")
blob = cursor.fetchone()
output_file.write(blob[0])
This works for text files, but all other file types (e.g. pdf, xlsx, etc.) are corrupted. Opening the exported file in notepad shows the same characters as casting the column as varchar in SQL Server.
How do I remedy this?