SQLite 3 insert binary image data with Python


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.