I am new to MySQL and pandas.
I have created a random dataframe:
data_frame = pd.DataFrame(np.random.randint(
0, 100, size=(100, 4)), columns=list('ABCD'))
And I want to insert it to my local MySQL database:
db = mysql.connector.connect(host="localhost",
user='root',
password='pw123',
database='analytics',
port='3307',
auth_plugin='mysql_native_password')
data_frame.to_sql(name='test',
con=db, if_exists='append', index=False)
But I get an error
pandas.io.sql.DatabaseError: Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': Not all parameters were used in the SQL statement
If I use sqlalchemy:
engine = create_engine("mysql://root:pw123@localhost/analytics")
con = engine.connect()
df = pd.DataFrame(['A', 'B'], columns=['new_tablecol'])
df.to_sql(name='new_table', con=con, if_exists='append')
con.close()
I get this error:
sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (1045, "Access denied for user 'root'@'localhost' (using password: YES)")
And I know I am using the right password. At this point I have no idea how I could insert my dataframes to a local database.
Thank you for your help.