How to store binary data in pewee BlobField

Question:

How can I store binary data io.BytesIO() in SQLlite DB using peewee?

When I try to store it in BlobField I’m getting following error:

ValueError: Value must be either a bytes, memoryview or BigBitFieldData instance.
Asked By: Leopoldo

||

Answers:

It appears that you’re not actually using a BlobField. However, to store data from a BytesIO object into an actual BlobField, you can:

# io.BytesIO.getvalue() method should return bytes.
some_model.blob_field = bytesio_obj.getvalue()
Answered By: coleifer

Building on the author’s answer, I am putting a more end-to-end example here for people who aren’t familiar with this the io

with open("file.parquet.gzip", "rb") as f:
    bytesio_obj = io.BytesIO(f.read())
    binary = bytesio_obj.getvalue()

some_model.blob_field = binary

Source: https://stackoverflow.com/a/59365168/5739514

Answered By: Kermit
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.