gpt4 book ai didi

python-3.x - 在 Python3 中使用 SQLAlchemy 在 sqlite3 数据库中存储和读取文件

转载 作者:行者123 更新时间:2023-12-03 19:37:47 25 4
gpt4 key购买 nike

我想将一个文件(例如 myfile.dat)从我的硬盘驱动器存储并读取到一个 sqlite3 数据库中,使用 Python3 通过 SQLAlchemy 进行访问。

我想为 Person-table 的每一行存储一张图片。我只想在显示那个人的数据时在 GUI 中显示该图片。

最佳答案

只需从硬盘读取二进制模式的文件并将其作为 BLOB 存储到数据库。

 with open('image.png', 'rb') as f:
fcontent = f.read()

从数据库中获取 BLOB(如 image)并将其提供给 Python 字节流。然后你可以使用它,例如作为 wxPython 中的 Stream-Object

 # read the BLOB as 'image' from database
# and use it
stream = io.BytesIO(image)
image = wx.Image(stream)
bitmap = wx.Bitmap(image)

关于python-3.x - 在 Python3 中使用 SQLAlchemy 在 sqlite3 数据库中存储和读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32935523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com