0

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'

1
  • I don't really know anything about this since I've never tried this but I read something about this that storing images in databases can be slow so people use filesystems, I dunno if this is helpful just wanted to put it out there
    – Ojou Nii
    Jun 16 '20 at 19:27
0

I finally figured it out after writing out all of my errors and then trying something completely different.

def place():
"""place an ad"""
if request.method == "GET":
    return render_template("place.html")

if request.method == "POST":

    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")

    sqliteConnection = sqlite3.connect('finance.db')
    cursor = sqliteConnection.cursor()
    query = cursor.execute("SELECT * FROM food ORDER BY post_time DESC LIMIT 1")

    for row in query:
        pic = row[4]
        writeTofile(pic, "dan.jpg")
    sqliteConnection.commit()
    cursor.close()

return render_template("placed.html")

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .