I would like to convert a pandas data-frame df
to an sql database and sent it to a sql-server. This is what I have done so far...
import sqlalchemy
import pickle
import os
server = 'HostAddres'
db = 'test_db'
login = 'Username'
passwd = 'Password'
engine_str = 'mysql+pymysql://{}:{}@{}/{}'.format(login, passwd, server, db)
engine = sqlalchemy.create_engine(engine_str, echo=False, encoding='utf-8')
df.to_sql(con=engine, name = "df_test", if_exists='replace', index=False)
OperationalError: (pymysql.err.OperationalError) (1045, "Access denied for user 'USER'@'HostAdress' (using password: YES)") (Background on this error at: http://sqlalche.me/e/e3q8)
Anyone knows how to fix this or what I am doing wrong ?
the create engine string should look like:
dialect+driver://username:password@host :port /database (don't include the spaces)
so theoretically your script should be:
server = 'HostAddres'
db = 'test_db'
login = 'Username'
passwd = 'Password'
port = '8888'
engine_str = 'mysql+pymysql://{}:{}@{}:{}/{}'.format(login, passwd, server,port, db)
engine = sqlalchemy.create_engine(engine_str, echo=False, encoding='utf-8')
df.to_sql(con=engine, name = "df_test", if_exists='replace', index=False)
As for the "to_sql" part itself there seems to be nothing wrong with it.
It's probably what i said in the comments and you only need to make sure you have permission to create or modify that database.