如何在python中通过psycopg2模块取消存储在postgresql中的二进制数据?

时间:2012-12-02 11:16:43

标签: python postgresql pickle psycopg2

我正在使用cPickle和psycopg2将一些向量存储到数据库中。 这是我存储二进制数据的代码

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()

但是当我使用fetchall()加载我的数据时,类型是缓冲区。我找不到如何将此缓冲区对象恢复到列表(vec)。

这是我获取数据的方式

db.cur.execute("SELECT * FROM feature_vector;")
m = db.cur.fetchall()

结果如下所示

[(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>),...

当我尝试使用cPickle.loads(m [0] [1])时,它将返回错误消息

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be string, not buffer

1 个答案:

答案 0 :(得分:6)

您可以创建自定义类型转换器以自动将pickled值转换为Python:

import cPickle
obj = {'a': 10}
data = cPickle.dumps(obj, -1)

import psycopg2

def cast_pickle(data, cur):
    if data is None: return None
    return cPickle.loads(str(psycopg2.BINARY(data, cur)))

psycopg2.extensions.register_type(
    psycopg2.extensions.new_type(
        psycopg2.BINARY.values, 'BINARY-PICKLE', cast_pickle))

cnn = psycopg2.connect('')
cur = cnn.cursor()
cur.execute("select %s::bytea", [psycopg2.Binary(data)])
cur.fetchone()
# ({'a': 10},)