I'm trying to add data frame information to PostgreSQL's table.
But I don't know how to create an engine. It's very likely related to connection to SQL server, but I could not figure out how to create it.
The example I saw on the to_sql document is:
>>> from sqlalchemy import create_engine
>>> engine = create_engine('sqlite://', echo=False)
And what I tried is:
param_dic = {'host': 'localhost', 'database':'databasename', 'user':'username', 'password':'password' }
conn = pgsql.connect(**param_dic)
cursor = conn.cursor()
for filename in work_dir.glob('excel_filename.xlsx'):
df = pd.read_excel(filename)
df.to_sql('table name', conn, if_exists='replace')
conn.commit()
cursor.close()
engine = create_engine('sqlite://', echo=False)
this seems incomplete. normally you'd put the connection string here, not just "sqlite".
I guess what you're trying to do is
# note, put your actual credentials here
engine = create_engine('postgresql://scott:tiger@localhost/test', echo=False)
you can then create a connection based on this engine, like
with engine.connect() as con:
# etc...
df.to_sql('table name', conn, if_exists='replace')
# etc...
or just con = engine.connect()