Skip to content

Instantly share code, notes, and snippets.

@m-mizutani
Created August 19, 2011 03:13
Show Gist options
  • Save m-mizutani/1155940 to your computer and use it in GitHub Desktop.
Save m-mizutani/1155940 to your computer and use it in GitHub Desktop.
Insert binary data as array.array type to sqlite3 DB as BLOB in python
import sqlite3
import array
a = array.array('B', [3, 1, 2,5,41,44,231,212,0,66,67,68])
b = buffer(a.tostring())
conn = sqlite3.connect ('test.db')
conn.execute ('create table if not exists test (id integer primary key, b blob)')
conn.execute ('delete from test')
conn.execute ('insert into test (b) values (?)', (b,))
conn.commit ()
cur = conn.cursor ()
cur.execute ('select b from test')
r = cur.fetchone()
print 'returned value', r[0].__class__
print 'matching', ("OK" if r[0] == b else "NG")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment