一种是导入sqlalchemy包,另一种是导入psycopg2包。 具体用法如下(此处以postgre数据库举例) postgresql://用户名:密码@host:port/db_name 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)
#一种是导入sqlalchemy包,另一种是导入psycopg2包。
#具体用法如下(此处以postgre数据库举例)
#postgresql://用户名:密码@host:port/db_name
#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)
def write_to_table(df, table_name, schema_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 = schema_name)
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 {}.{} FROM STDIN HEADER DELIMITER '|' CSV".format(table_name, schema_name)
cursor.copy_expert(copy_cmd, string_data_io)
connection.connection.commit()
至此完成!!!