Problems inserting file data into sqlite database using python

Posted by tylerc230 on Stack Overflow See other posts from Stack Overflow or by tylerc230
Published on 2010-04-25T03:39:32Z Indexed on 2010/04/25 3:43 UTC
Read the original article Hit count: 193

Filed under:
|
|

I'm trying to open an image file in python and add that data to an sqlite table. I created the table using: "CREATE TABLE "images" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , "description" VARCHAR, "image" BLOB );"

I am trying to add the image to the db using:

imageFile = open(imageName, 'rb')
b = sqlite3.Binary(imageFile.read())
targetCursor.execute("INSERT INTO images (image) values(?)", (b,))
targetCursor.execute("SELECT id from images")
for id in targetCursor:
    imageid= id[0]

targetCursor.execute("INSERT INTO %s (questionID,imageID) values(?,?)" % table, (questionId, imageid))

When I print the value of 'b' it looks like binary data but when I call: 'select image from images where id = 1' I get '????' printed to the console. Anyone know what I'm doing wrong?

© Stack Overflow or respective owner

Related posts about python

Related posts about sqlite