小编And*_*sey的帖子

使用psycopg2转换器从PostgreSQL中检索bytea数据

我想以二进制(bytea)形式将Numpy数组存储在PostgreSQL数据库中.我可以在测试#1(见下文)中正常工作,但我不想在插入之前和之后选择之后操作数据数组 - 我想使用psycopg2的适配器和转换器.

这就是我现在所拥有的:

import numpy as np
import psycopg2, psycopg2.extras


def my_adapter(spectrum):
    return psycopg2.Binary(spectrum)

def my_converter(my_buffer, cursor):
    return np.frombuffer(my_buffer)


class MyBinaryTest():

    # Connection info
    user = 'postgres'
    password = 'XXXXXXXXXX'
    host = 'localhost'
    database = 'test_binary'


    def __init__(self):
        pass

    def set_up(self):

        # Set up
        connection = psycopg2.connect(host=self.host, user=self.user, password=self.password)

        connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)

        cursor = connection.cursor()
        try: # Clear out any old test database
            cursor.execute('drop database %s' % (self.database, ))
        except:
            pass

        cursor.execute('create database %s' % (self.database, ))
        cursor.close()
        connection.close()

        # Direct …
Run Code Online (Sandbox Code Playgroud)

python postgresql psycopg2

6
推荐指数
1
解决办法
7619
查看次数

标签 统计

postgresql ×1

psycopg2 ×1

python ×1