Problems inserting file data into sqlite database using python
- by tylerc230
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?