【问题标题】:Binary data is not getting stored in SQLite, returns null二进制数据未存储在 SQLite 中,返回 null
【发布时间】:2020-11-28 05:59:49
【问题描述】:

打印出数据给我:

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

【问题讨论】:

    标签: sqlite blob


    【解决方案1】:

    我相信您必须将插入内容包装到 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()
    
    

    更多详情请见here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-23
      • 1970-01-01
      • 1970-01-01
      • 2010-11-09
      • 1970-01-01
      • 1970-01-01
      • 2012-03-30
      • 1970-01-01
      相关资源
      最近更新 更多