I am using cPickle and psycopg2 to store some vectors into database. This is my code to store binary data
binary_vec = cPickle.dumps(vec, -1)
db.cur.execute('''
INSERT INTO feature_vector (vector, id)
VALUES (%s, %s);
''', (psycopg2.Binary(binary_vec), thread_id)
db.conn.commit()
However when I use fetchall() to load my data back, the type is buffer. I can't find how how to restore this buffer object back to a list (vec).
This is how I fetch the data
db.cur.execute("SELECT * FROM feature_vector;")
m = db.cur.fetchall()
The result looks like this
[(3169187, <read-only buffer for 0x1002b0f10, size 3462, offset 0 at 0x1004a7430>),
(3169275, <read-only buffer for 0x1002b0f50, size 3462, offset 0 at 0x1004a7570>),
(3169406, <read-only buffer for 0x1002b0f70, size 3462, offset 0 at 0x10140b0b0>),
(3169541, <read-only buffer for 0x10141c030, size 3462, offset 0 at 0x10140b2b0>),
(3169622, <read-only buffer for 0x10141c050, size 3462, offset 0 at 0x10140b3f0>),...
When I try to use cPickle.loads(m[0][1]), it will return the error message
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be string, not buffer
mysql -e 'select * from ...'
) that the data is indeed stored in the database? – Jakub M. Dec 2 '12 at 11:59str(the_buffer)
orbytes(the_buffer)
? – Bakuriu Dec 2 '12 at 21:04