Python 2.7, sqlite3, ValueError: Failed to convert BLOB to buffer

I am trying to save a BLOB created from an integer array (i.e. a packed array of integers) in SQLite DB. The script below gives the following trace. As far as I understand from the Python 2.7 sqlite3 documentation, it should be possible to insert a buffer object into a table where it is supposed to be saved as a BLOB. However, I cannot complete this work. (FWIW, isinstance (b, buffer) print True if inserted in the script, so I really create a buffer object.)

Any suggestions?

Thanks, -P.

Traceback (most recent call last):
  File "example.py", line 13, in <module>
    conn.execute( 'insert into foo values (?)', (b,) ) # <=== line 14
ValueError: could not convert BLOB to buffer

import sqlite3
import sys
import array

ar = array.array( 'I' )
ar.extend( [1,0,3,11,43] )
b = buffer( ar )

conn = sqlite3.connect( ':memory:' )
conn.execute( 'create table foo( bar BLOB )' )
conn.commit()

conn.execute( 'insert into foo values (?)', (b,) ) # <=== line 14
conn.commit()
+3
source share
1 answer

Cristian Ciupitu , bytes(ar) __str__ . ar.tostring().

array.fromstring - , .fromstring(...).

+2

Source: https://habr.com/ru/post/1770643/


All Articles