I am getting an Invalid or Unsupported Image Format
when I open an image stored and retrieved in SQLite. When I input the convertToBinaryData
file into the writeTofile
function then the photo is retrieved so this is definitely something to do with the SQL. The only place I see this error message on Google is here in CS50 so...
Can someone assist in formatting this correctly?
Below are the options I have tried and the errors I have gotten.
def convertToBinaryData(filename):
#Convert digital data to binary format
with open(path, 'rb') as file:
blobData = file.read()
return blobData
def writeTofile(data, filename):
# Convert binary data to proper format and write it on Hard Disk
with open(filename, 'wb') as file:
advert = file.write(data)
I have tried the follow four options and none of them work.
'''Version 1'''
placing = "INSERT INTO food (category, title, description, shipping, photo, county, area,
post_time, UID) VALUES (?,?,?,?,?,?,?,CURRENT_TIMESTAMP,?)"
ad_photo = convertToBinaryData(request.form.get("photo"))
db.execute(placing, (session["user_id"], request.form.get("category"), request.form.get("title"),
request.form.get("description"), request.form.getlist("option[]"), ad_photo,
request.form.get("county"), request.form.get("area")))
row = db.execute('SELECT photo FROM food')
print (str(row[0]))
writeTofile(row[0], "dan.jpg")
ERROR: file.write(data) TypeError: a bytes-like object is required, not 'dict'
'''Version 2'''
placing = "INSERT INTO food (category, title, description, shipping, photo, county, area,
post_time, UID) VALUES (?,?,?,?,?,?,?,CURRENT_TIMESTAMP,?)"
ad_photo = convertToBinaryData(request.form.get("photo"))
db.execute(placing, (session["user_id"], request.form.get("category"), request.form.get("title"),
request.form.get("description"), request.form.getlist("option[]"), ad_photo,
request.form.get("county"), request.form.get("area")))
row = db.execute('SELECT photo FROM food')
print (row[0])
writeTofile(row[0], "dan.jpg")
ERROR: file.write(data) TypeError: a bytes-like object is required, not 'dict'
'''Version 3'''
placing = "INSERT INTO food (UID, category, title, description, shipping, photo, county, area,
post_time) VALUES (?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP)"
ad_photo = convertToBinaryData(request.form.get("photo"))
db.execute(placing, (session["user_id"], request.form.get("category"), request.form.get("title"),
request.form.get("description"), request.form.getlist("option[]"), ad_photo,
request.form.get("county"), request.form.get("area")))
row = db.execute('SELECT photo FROM food')
print (str(row[0]))
mystring = str(row[0])
b = mystring.encode('utf-8')
writeTofile(b, "dan.jpg")
ERROR: no error but JPEG that is created is "Invalid or Unsupported Image Format"
'''Version 4'''
sqliteConnection = sqlite3.connect('finance.db')
cursor = sqliteConnection.cursor()
placing = "INSERT INTO food (category, title, description, shipping, photo, county, area, post_time, UID) VALUES (?,?,?,?,?,?,?,CURRENT_TIMESTAMP,?)"
ad_photo = convertToBinaryData(request.form.get("photo"))
# Convert data into tuple format
data_tuple = (request.form.get("category"), request.form.get("title"), request.form.get("description"), request.form.get("option[]"), ad_photo, request.form.get("county"), request.form.get("area"), session["user_id"])
cursor.execute(placing, data_tuple)
sqliteConnection.commit()
print("Image and file inserted successfully as a BLOB into a table")
cursor.execute("SELECT photo from food")
record = cursor.fetchone()
writeTofile(record, "dan.jpg")
cursor.close()
ERROR: TypeError: a bytes-like object is required, not 'tuple'