python连接postgresql数据库及操作
- 1.python连接数据库
- 1.1 安装相关库
- 1.1 创建连接
- 1.1 关闭连接
- 2.python postgresql创建表
- 2.1 创建连接
- 2.2 创建事务提交函数
- 2.3 创建表语句并调用函数
- 2.4 关闭连接
- 3.python postgresql批量插入表数据
- 3.1 创建连接
- 3.2 创建插入表数据函数
- 3.3 读取表数据,调用函数
- 3.4 关闭连接
1.python连接数据库
1.1 安装相关库
复制代码
1
2
3pip install psycopg2 pip install pandas
1.1 创建连接
复制代码
1
2
3
4
5import psycopg2 import pandas as pd conn = psycopg2.connect(database="postgres", user="postgres", password="", host="127.0.0.1", port="5432", client_encoding='utf-8') cursor = conn.cursor()
1.1 关闭连接
复制代码
1
2
3cursor.close() conn.close()
2.python postgresql创建表
2.1 创建连接
复制代码
1
2
3
4
5import psycopg2 import pandas as pd conn = psycopg2.connect(database="mapdb", user="postgres", password="", host="127.0.0.1", port="5432", client_encoding='utf-8') cursor = conn.cursor()
2.2 创建事务提交函数
复制代码
1
2
3
4
5
6
7
8
9def func_conn(conn,cursor,sql): try: cursor.execute(sql) conn.commit() print("successfully") except Exception as e: print('error',e) conn.rollback()
2.3 创建表语句并调用函数
复制代码
1
2
3
4
5
6sql= """CREATE TABLE climate (ID INT PRIMARY KEY NOT NULL, date date NOT NULL );""" func_conn(conn,cursor,sql)
2.4 关闭连接
复制代码
1
2
3cursor.close() conn.close()
3.python postgresql批量插入表数据
3.1 创建连接
复制代码
1
2
3
4
5
6import psycopg2 import pandas as pd from io import StringIO conn = psycopg2.connect(database="mapdb", user="postgres", password="", host="127.0.0.1", port="5432", client_encoding='utf-8') cursor = conn.cursor()
3.2 创建插入表数据函数
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16conn = psycopg2.connect(host='127.0.0.1', user="postgres", password="", database="mapdb") cur = conn.cursor() def insert_table(data,table_name): try: f = StringIO() data.to_csv(f, sep='t', index=False, header=False) f.seek(0) cur.copy_from(f,table_name,columns=data.columns.to_list()) # 将内存对象f中的数据写入数据库,参数columns为所有列的元组 conn.commit() f.close() print('成功写入数据库') except Exception as e: print(e) conn.rollback() f.close()
3.3 读取表数据,调用函数
复制代码
1
2
3
4# 注意:插入数据时,数据库中需要存在这张表 data=pd.read_csv('data/data.csv') insert_table(data,'table_name')
3.4 关闭连接
复制代码
1
2
3cursor.close() conn.close()
最后
以上就是单薄斑马最近收集整理的关于python连接postgresql数据库及操作的全部内容,更多相关python连接postgresql数据库及操作内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复