buhtz
buhtz

Reputation: 12202

Store and read a file in a sqlite3 database with SQLAlchemy in Python3

I want to store and read a file (e.g. myfile.dat) from my hard drive into a sqlite3 database accessing over SQLAlchemy with Python3.

I want to store one picture for each row of Person-table. I just want to show that picture in a GUI when the data of that Person are shown.

Upvotes: 3

Views: 1054

Answers (1)

buhtz
buhtz

Reputation: 12202

Simply read the file in binary-mode from hard drive and store it as a BLOB to the database.

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

Get the BLOB (as image) from database and feed it to a Python-byte stream. Then you can use it e.g. as a Stream-Object in wxPython.

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

Upvotes: 3

Related Questions