Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 590
buhtz Avatar asked Aug 13 '26 11:08

buhtz


1 Answers

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)
like image 139
buhtz Avatar answered Aug 15 '26 06:08

buhtz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!