Posted By

deepdown on 11/28/08


Tagged

image python binary blob SQLite


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

ricardodapaz
gartenstuhl


SQLite 3 insert binary image data with Python


 / Published in: Python
 

  1. from pysqlite2 import dbapi2 as sqlite
  2.  
  3. try:
  4. from sqlite import encode, decode
  5. except ImportError:
  6. import base64
  7. sqlite.encode = base64.encodestring
  8. sqlite.decode = base64.decodestring
  9. else:
  10. sqlite.encode = encode
  11. sqlite.decode = decode
  12.  
  13. imagedata = open('myfile.jpg', "rb").read()
  14. con = sqlite.connect("mydbfile")
  15. cur = con.cursor()
  16. cur.execute('INSERT INTO image(binarydata) VALUES (?)', (sqlite.encode(sqlite.Binary(imagedata)),))
  17. con.commit()
  18. con.close()

Report this snippet  

You need to login to post a comment.