python rsa加密之后byte类型存储到数据库中_Python numpy插入、读取至postgreSQL数据库中bytea类型字段...

安装psycopg2模块,此模块用于连接PostgreSQL数据库

​pip install psycopg2

# -*- coding: utf-8 -*-

import psycopg2

import numpy as np

import json

def insertOperate():

conn = psycopg2.connect(database="openfire", user="postgres", password="postgres", host="192.168.3.202", port="5432")

cursor = conn.cursor()

x = np.arange(12).reshape(2, 6)

# 建表

cursor.execute('create table insightface.t_test ("data" bytea)')

insert_sql = "insert into insightface.t_test (\"data\") values ('%s')"

insert_sql = insert_sql % json.dumps(x.tolist())

print(insert_sql)

# 插入

cursor.execute(insert_sql)

# 提交

conn.commit()

cursor.close() # 关闭Cursor

conn.close() # 关闭数据库

def selectOperate():

conn = psycopg2.connect(database="openfire", user="postgres", password="postgres", host="192.168.3.202", port="5432")

cursor = conn.cursor()

cursor.execute("select data from insightface.t_test")

rows = cursor.fetchall()

for row in rows:

print (row[0], '\n')

print(type(row[0]))

print(bytes.decode(bytes(row[0])))

print(type(bytes.decode(bytes(row[0]))))

my_list = json.loads(bytes.decode(bytes(row[0])))

# List转numpy.array

temp = np.array(my_list)

print(type(temp))

print(temp.shape)

print(temp)

conn.close()

if __name__ == '__main__':

insertOperate()

selectOperate()

56f19340ba63c9b18648c180ff18f6c9.png

insert into insightface.t_test ("data") values ('[[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]]')

[[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]]

(2, 6)

[[ 0 1 2 3 4 5]

[ 6 7 8 9 10 11]]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值