Founder & Programmer at HackersFriend April 3, 2020, 10:18 p.m. ⋅ 1446 views

How to upload file to SQL server using pyodbc python

To upload files to SQL server using pyodbc we use pyodbc.Binary() function. We open file as binary file using python open() function, and read it into a variable. Then we pass this to pyodbc.Binary()  after that, we insert this value as varbinary  in FileTable. 

Here is how to implement it.

# Get connection
connection = pyodbc.connect(...)
filename = 'MyFile.pdf'
insert = 'insert into FileTable (name, document) values (?,?)'

# open file as binary and read into a variable
with open(filename, 'rb') as f:
    bindata = f.read()

# build query
binparams = ('MyFile.pdf', pyodbc.Binary(bindata))

# insert binary
connection.cursor().execute(insert, binparams)


Go back to feed

HackersFriend Updates

View more


Nov. 28, 2018, 5:30 p.m.

Python from zero to hero

place Delhi

View details

Aug. 13, 2018, 5:30 p.m.

Python from zero to hero

place Bangalore ( HackersFriend office BTM Layout)

View details