从SQLite检索图像,“图像格式无效或不受支持”

问题描述 投票:1回答:1

我正在使用Cloud9 IDE,当我打开在SQLite中存储和检索的图像时,得到的是"Invalid or Unsupported Image Format"。当我将convertToBinaryData文件输入到writeTofile函数中时,便会检索照片,因此这绝对与SQL有关。

有人可以协助您正确格式化吗?

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)

@app.route("/place", methods=["GET", "POST"])
@login_required
def place():
if request.method == "GET":
    return render_template("place.html")

if request.method == "POST":

我已经尝试了以下四个选项,但它们都不起作用。

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

错误:file.write(数据)TypeError:需要一个类似字节的对象,而不是'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")

错误:file.write(数据)TypeError:需要一个类似字节的对象,而不是'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")

错误:没有错误,但是创建的JPEG是“无效或不受支持的图像格式”

'''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()

错误:TypeError:需要一个类似字节的对象,而不是“元组”

python-3.x sqlite binary blob
1个回答
0
投票

好吧,我在写完所有错误然后尝试完全不同的方法后终于弄清楚了。感谢您的共鸣。

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")
© www.soinside.com 2019 - 2024. All rights reserved.