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

insert into insightface.t_test ("data") values ('[[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]]')
<memory at 0x000000000973EC48> <class 'memoryview'>
[[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]]
<class 'str'>
<class 'numpy.ndarray'>
(2, 6)
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]]

最新文章

  1. GPS定位为什么要转换处理?高德地图和百度地图坐标处理有什么不一样?
  2. 抓包工具charles的使用
  3. HttpSession--会话
  4. fastjson解析任意json
  5. Python基本数据类型之list列表
  6. Elasticseach部分语法总结
  7. ArcGis学习教程免费版在线观看
  8. 跟我一起学习ASP.NET 4.5 MVC4.0(六)(转)
  9. mybatis update语句参数正常, 数据没有更新
  10. ubuntu主机名修改
  11. 手动编译 Nginx 并安装 VeryNginx
  12. 聊一聊C#的Equals()和GetHashCode()方法
  13. 公网定制化yum仓库部署
  14. 盖茨基金会:如何使用Python拯救生命
  15. 案例学python——案例三:豆瓣电影信息入库
  16. poj 3352 Road Construction(边双连通分量+缩点)
  17. centos7 安装 jdk1.8
  18. Ehcache.xml 配置及属性说明
  19. 在线调整InnoDB Buffer Pool Size
  20. JQuery 中的Show方法

热门文章

  1. spring 接口校验参数(自定义注解)
  2. FP AUTO节点ZPP002M执行卡住解决
  3. 好题Islands
  4. [CSP-S模拟测试]:格式化(贪心)
  5. 在HTML标签元素中,绑定JS函数
  6. 【重点突破】—— UniApp微信小程序开发教程学习Three
  7. 2017-03-04 idea破解
  8. gson转换json到bean时重命名
  9. Win7上防火墙开放FTP服务以及ping解决方案
  10. 阶段3 1.Mybatis_02.Mybatis入门案例_3.mybatis注解开发和编写dao实现类的方式