Trouble storing numpy array in sqlite3 with python Trouble storing numpy array in sqlite3 with python sqlite sqlite

Trouble storing numpy array in sqlite3 with python


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:    bufferexcept NameError:    buffer = bytes