首页 > 解决方案 > 二进制数据未存储在 SQLite 中,返回 null

问题描述

打印出数据给我:

\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00\x84\x00\t\x06\x07\x13\x13\x12\x15\x13\x13\x13\x16\x16\x15\x17\x19\x1a\x1b\x17\x16\x18\x18

所以二进制数据是存在的。但是,当我尝试使用此代码插入值时:

        for i in check_row:
            if i[0] == 0:
                db.execute("INSERT INTO profiles (profile_id, city, country, interest, bio, picture) VALUES (:user, :city, :country, :interest, :bio, :picture)",
                [user, city, country, interest, bio, empPicture])
                conn.commit()
                flash("profile completed")
                return redirect("/")
            else:
                db.execute("UPDATE profiles SET city = :city, country = :country, interest = :interest, bio = :bio, picture = :picture WHERE profile_id = :profile_id",
                [city, country, interest, bio, user, empPicture])
                conn.commit()
                flash("profile updated")
                return redirect("/profile")

empPicture是保存二进制数据的变量。但是,这不会返回错误,数据永远不会输入到图片 BLOB 列中,它只显示 NULL 并在运行时打印出 None。这是我的桌子:

CREATE TABLE "profiles" ( "profile_id" INTEGER, "city" TEXT VARCHAR(255), "country" TEXT VARCHAR(255), "interest" TEXT VARCHAR(255), "bio" TEXT VARCHAR(255), "picture" BLOB )

标签: sqliteblob

解决方案


我相信您必须将插入内容包装sqlite3.Binary如下:

db.execute("INSERT INTO profiles (profile_id, city, country, interest, bio, picture) VALUES (:user, :city, :country, :interest, :bio, :picture)",
[user, city, country, interest, bio, sqlite3.Binary(empPicture)])
conn.commit()

更多细节可以在这里找到。


推荐阅读