I try to build an sql query string with .format() and binary content (injections are no problem). The minimal example needs an example image in the same path.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sqlite3 as lite
import sys
try:
con = lite.connect(':memory:')
cur = con.cursor()
binary = lite.Binary(open("woman.jpg", "rb").read())
cur.execute("CREATE TABLE 'Images' ('Data' BLOB)")
cur.execute("INSERT INTO Images(Data) VALUES (?)", (binary,) )
query = "INSERT INTO Images(Data) VALUES ({0})".format(binary)
cur.execute(query) # <- doesn't work obviously
con.commit()
except lite.Error, e:
if con:
con.rollback()
print "Error %s:" % e.args[0]
sys.exit(1)
finally:
if con:
con.close()
Just like the insert image example from http://zetcode.com/db/sqlitepythontutorial/ but with .format() query generation
Is there a way to achieve this with .format()? I want to use a consistent way of inserting data into the db.
Thank you.