2

The python sqlite3 documentation gives this example for inserting parameters into an SQL query.

for t in [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
          ('2006-04-05', 'BUY', 'MSOFT', 1000, 72.00),
          ('2006-04-06', 'SELL', 'IBM', 500, 53.00),
         ]:
    c.execute('insert into stocks values (?,?,?,?,?)', t)

That's fine for text and numbers. What if one of the fields is a binary BLOB, such as a JPEG image? How does one insert a binary file?

2
  • What do you think you mean by "binary file"? And what table definition do you have? Commented Jul 29, 2011 at 23:52
  • 1
    If you're on Python2.x strings and bytes are the same thing... Commented Jul 29, 2011 at 23:58

1 Answer 1

2

Insert binary data just as you would for any other type of field:

contents = open(image_path, "rb").read()
c.execute('insert into images values (?, ?)', (image_path, contents))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.