Skip to content Skip to sidebar Skip to footer

Trouble Storing Numpy Array In Sqlite3 With Python

I'm trying to follow the example shown as the top answer here: Python insert numpy array into sqlite3 database At first I thought it was my code, but I've tried copying and pasti

Solution 1:

The only problem with unutbu's code is that his adapt_array raises an exception in Python 3:

def adapt_array(arr):
    out = io.BytesIO()
    np.save(out, arr)
    out.seek(0)
    # http://stackoverflow.com/a/3425465/190597 (R. Hill)
    return buffer(out.read())

That's because buffer doesn't exist in 3.x. And it isn't actually doing anything useful here in 2.x, so you can just remove it. Just replace that last line with:

return out.read()

And now, everything else works perfectly.

If you need compatibility with older 2.x and also with 3.x (I'm not sure if there are any versions where this overlaps, but there might be…), you can instead fix it by doing this at the top of the module:

try:
    buffer
except NameError:
    buffer = bytes

Post a Comment for "Trouble Storing Numpy Array In Sqlite3 With Python"