如何使用python-sqlite3将图片存储在表中? [英] How do I store a picture in a table with python-sqlite3?

查看:813
本文介绍了如何使用python-sqlite3将图片存储在表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图片存储在sqlite3表中。我正在使用python和sqlite3。
请让我知道您是否有示例代码或如何将图片保存到sqlite3表。

I'm trying to store a picture in sqlite3 table. I'm using python and sqlite3. Please let me know if you have a sample code or how to save a picture to a sqlite3 table.

推荐答案

您可以将其编码为base64字符串(如Yogesh所述),也可以尝试存储二进制文件。

You can either encode it as a base64 string, like Yogesh mentioned, or try to store the binary.

import sqlite3
import base64

conn = sqlite3.connect('example.db')
c = conn.cursor()
c.execute('''CREATE TABLE images (image text)''')

# binary
c.execute("INSERT INTO images VALUES ({})".format(sqlite3.Binary(file.read())))

# base64
c.execute("INSERT INTO images VALUES ({})".format(base64.b64encode(file.read())))

conn.commit()
conn.close()

这篇关于如何使用python-sqlite3将图片存储在表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆