4000 python 快速写入postgresql数据库方法
2018-03-19 11:33
162 查看
一种是导入sqlalchemy包,另一种是导入psycopg2包。
具体用法如下(此处以postgre数据库举例)
第一种:
import io
import pandas as pd
from sqlalchemy import create_engine
db_engine = create_engine('postgresql://***:***@***:***/***')# 初始化引擎
string_data_io = io.StringIO()
df.to_csv(string_data_io, sep='|', index=False)
pd_sql_engine = pd.io.sql.pandasSQL_builder(db_engine)
table = pd.io.sql.SQLTable(table_name, pd_sql_engine, frame=df,
index=False, if_exists=if_exists,schema = 'goods_code')
table.create()
string_data_io.seek(0)
string_data_io.readline() # remove header
with db_engine.connect() as connection:
with connection.connection.cursor() as cursor:
copy_cmd = "COPY goods_code.%s FROM STDIN HEADER DELIMITER '|' CSV" %table_name
cursor.copy_expert(copy_cmd, string_data_io)
connection.connection.commit()
具体用法如下(此处以postgre数据库举例)
第一种:
# 导入包 from sqlalchemy import create_engine import pandas as pd from string import Template engine = create_engine("oracle://user:pwd@***:***/racdb", echo=False) # 初始化引擎 engine = create_engine('postgresql+psycopg2://' + pg_username + ':' + pg_password + '@' + pg_host + ':' + str( pg_port) + '/' + pg_database) query_sql = """ select * from $arg1 """ query_sql = Template(query_sql) # template方法 df = pd.read_sql_query(query_sql .substitute(arg1=tablename),engine) # 配合pandas的方法读取数据库值 # 配合pandas的to_sql方法使用十分方便(dataframe对象直接入库) df.to_sql(table, engine, if_exists='replace', index=False) #覆盖入库 df.to_sql(table, engine, if_exists='append', index=False) #增量入库注意:上述df.to_sql的方法实在是太慢太慢了,千万的数据chunksize设置为万,上传了5个小时 郁闷。查资料后得知以下方法:速度极快!!!!!def write_to_table(df, table_name, if_exists='fail'):
import io
import pandas as pd
from sqlalchemy import create_engine
db_engine = create_engine('postgresql://***:***@***:***/***')# 初始化引擎
string_data_io = io.StringIO()
df.to_csv(string_data_io, sep='|', index=False)
pd_sql_engine = pd.io.sql.pandasSQL_builder(db_engine)
table = pd.io.sql.SQLTable(table_name, pd_sql_engine, frame=df,
index=False, if_exists=if_exists,schema = 'goods_code')
table.create()
string_data_io.seek(0)
string_data_io.readline() # remove header
with db_engine.connect() as connection:
with connection.connection.cursor() as cursor:
copy_cmd = "COPY goods_code.%s FROM STDIN HEADER DELIMITER '|' CSV" %table_name
cursor.copy_expert(copy_cmd, string_data_io)
connection.connection.commit()
相关文章推荐
- Python连接PostgreSQL数据库的方法
- Python MySQLdb写入数据库乱码问题解决方法
- 将爬取到的数据(用Python)写入PostgreSQL数据库
- Python实现读写sqlite3数据库并将统计数据写入Excel的方法示例
- Python连接PostgreSQL数据库的方法
- Python实现连接postgresql数据库的方法分析
- python DB-API数据库常用对象及方法,postgresql为例
- python操作postgresql数据库
- 如何快速导出数据库为EXCEL文档(另一种方法)
- Python3 大型网络爬虫实战 004 — scrapy 大型静态商城网站爬虫项目编写及数据写入数据库实战 — 实战:爬取淘宝
- Ubuntu下PostgreSQL数据库集群(PL/Proxy)配置方法
- Python实现监控程序执行时间并将其写入日志的方法
- ORACLE 快速批量导入文本数据到数据库(sqlldr工具)方法与分析
- 自己写的Python数据库连接类和sql语句拼接方法
- PostgreSQL数据库创建、删除方法
- postgreSQl 连接数据库时 出现无法连接的现象就解决方法
- python操作数据库PostgreSQL
- Python连接mssql数据库编码问题解决方法
- Python使用win32com模块实现数据库表结构自动生成word表格的方法
- MySQL快速复制数据库的方法