https://www.geeksforgeeks.org/how-to-insert-a-pandas-dataframe-to-an-existing-postgresql-table/#
https://stackoverflow.com/questions/62688256/sqlalchemy-exc-nosuchmoduleerror-cant-load-plugin-sqlalchemy-dialectspostgre
import psycopg2
import pandas as pd
from sqlalchemy import create_engine
conn_string = 'postgresql://user:password@host/data1'
db = create_engine(conn_string)
conn = db.connect()
# our dataframe
data = {'Name': ['Tom', 'dick', 'harry'],
'Age': [22, 21, 24]}
# Create DataFrame
df = pd.DataFrame(data)
df.to_sql('data', con=conn, if_exists='replace',
index=False) # data 表名
conn = psycopg2.connect(conn_string
)
conn.autocommit = True
cursor = conn.cursor()
sql1 = '''select * from data;'''
cursor.execute(sql1)
for i in cursor.fetchall():
print(i)
# conn.commit()
conn.close()
标签:string,数据库,保存,cursor,import,data,pandas,conn
From: https://www.cnblogs.com/qianxunman/p/17654873.html