Created
April 9, 2024 07:15
-
-
Save calina-c/79d7294b42080fe581a81e00ced6f587 to your computer and use it in GitHub Desktop.
Save and load binary np array into a BLOB database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import binascii | |
import duckdb | |
import io | |
import numpy as np | |
def makeblob(arr): | |
f = io.BytesIO() | |
np.save(f, arr) | |
f.seek(0) | |
blob = f.read() | |
return blob.hex() | |
def readblob(blob): | |
f = io.BytesIO() | |
f.write(blob) | |
f.seek(0) | |
data = np.load(f) | |
return binascii.unhexlify(data) | |
duckdb_conn = duckdb.connect( | |
database="duck_database.db" | |
) | |
duckdb_conn.execute("""CREATE TABLE IF NOT EXISTS binstates ( | |
blobby BLOB, | |
) | |
""") | |
array_obj = np.array([1, 2, 3, 4, 5]) | |
query = f"""INSERT INTO binstates VALUES ('{blob_obj}')""" | |
duckdb_conn.execute(query) | |
result = duckdb_conn.execute("SELECT * FROM binstates;").fetchall() | |
blob_rec = binascii.unhexlify(result[0][0]) | |
blob_obj_rec = readblob(blob_rec) | |
# blob_obj == blob_obj_rec |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment