sqlite - 二进制数据未存储在 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 )
解决方案
我相信您必须将插入内容包装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()
更多细节可以在这里找到。
推荐阅读
- jquery - jQuery datepicker - 将 mindDate 增加一天
- javascript - 有没有办法将线性渐变与 HSLA 一起使用?
- javascript - 我需要一个 JavaScript 表单中的元素(在 React 中)来选择多个项目
- django - 在 Django 中刷新数据库
- ffmpeg - 如何将一个图像序列转换为多个 mp4 视频文件?
- java - 如何在 Java 中旋转和绘制 BufferedImage?
- python - 沿其最后一维对数组进行排序,并撤消排序
- html - 如何创建跨越 3 个垂直 div 元素的徽标图像
- country-codes - 国家代码 API
- javascript - React 浅拷贝仍然触发重新渲染?