Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8349223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:55:46+00:00 2026-06-09T07:55:46+00:00

I am trying to insert binary data into a column of image datatype in

  • 0

I am trying to insert binary data into a column of image datatype in a SQL Server database. I know varbinary(max) is the preferred data type, but I don’t have rights to alter the schema.

Anyhow, I am reading the contents of a file and wrapping it in pyodbc.Binary() as below:

f = open('Test.ics', 'rb')
ablob = f.read().encode('hex')
ablob = pyodbc.Binary(ablob)

When I print repr(ablob) I see the correct value bytearray(b'424547494e3a5 . . . (ellipsis added).

However, after inserting

insertSQL = """insert into documents(name, documentType, document, customerNumber) values(?,?,?,?)"""
cur.execute(insertSQL, 'test200.ics', 'text/calendar', pyodbc.Binary(ablob), 1717)

The value of the document column is 0x343234353 . . . which appears as if the hexadecimal data was converted to ASCII character codes.

I thought wrapping the value in pyodbc.Binary() would take care of this? Any help would be greatly appreciated.

I am using Python 2.7 and SQL Server 2008 R2 (10.50).

Edit:

beargle kindly pointed out that I was needlessly calling encode(‘hex’), which was leading to my issue. I believe this must have been coercing the data into a string (although a fuller explanation would be helpful).

Working code:

ablob = pyodbc.Binary(f.read())
cur.execute(insertSQL, 'test200.ics', 'text/calendar', ablob, 1717)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T07:55:48+00:00Added an answer on June 9, 2026 at 7:55 am

    First make sure you use with open(..) to read the file (another example). This automatically closes file objects when they are exhausted or an exception is raised.

    # common vars
    connection = pyodbc.connect(...)
    filename = 'Test.ics'
    insert = 'insert into documents (name, documentType, document, customerNumber)'
    
    # without hex encode
    with open(filename, 'rb'):
        bindata = f.read()
    
    # with hex encode
    with open(filename, 'rb'):
        hexdata = f.read().encode('hex')
    
    # build parameters
    binparams = ('test200.ics', 'text/calendar', pyodbc.Binary(bindata), 1717)
    hexparams = ('test200.ics', 'text/calendar', pyodbc.Binary(hexdata), 1717)
    
    # insert binary
    connection.cursor().execute(insert, binparams)
    connection.commit()
    
    # insert hex
    connection.cursor().execute(insert, hexparams)
    connection.commit()
    
    # print documents
    rows = connection.cursor().execute('select * from documents').fetchall()
    for row in rows:
        try:
            # this will decode hex data we inserted
            print str(row.document).decode('hex')
        # attempting to hex decode binary data throws TypeError
        except TypeError:
            print str(row.document)
    

    I’m guessing you are getting the 0x343234353... data by looking at results in Management Studio:

    SQL Server Management Studio results

    This doesn’t mean the data is stored this way, it’s just the way Management Studio represents image, text, ntext, varbinary, etc. datatypes in the result pane.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to insert some binary data into database using 'mysql' gem in ruby.
I have a varbinary(MAX) field in a SQL Server 2005 database. I'm trying to
I'm trying to insert some Binary data into a MySQL database without using prepared
I'm trying to insert binary data (a whirlpool hash) into a PG table and
Trying to insert 315K Gif files into an Oracle 10g database. Everytime I get
Am trying to insert XML into XML Column.. getting following error: . Msg 6819,
While trying to insert data to my SQl db i get the following error
iam trying insert rows into a sql table in java. i need to insert
I was trying to insert some data into a table and I got the
Im trying to insert data from a textbox to my database, and it throwing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.