I am trying to store data retrieved from a website into MySQL database via a pandas data frame. However, when I make the function call df.to_sql()
, the compiler give me an error message saying: AttributeError: 'Connection' object has no attribute 'connect'
. I tested it couple times and I am sure that there is neither connection issue nor table existence issue involved. Is there anything wrong with the code itself? The code I am using is the following:
from sqlalchemy import create_engine, text
import pandas as pd
import mysql.connector
config = configparser.ConfigParser()
config.read('db_init.INI')
password = config.get("section_a", "Password")
host = config.get("section_a", "Port")
database = config.get("section_a", "Database")
engine = create_engine('mysql+mysqlconnector://root:{0}@{1}/{2}'.
format(password, host, database),
pool_recycle=1, pool_timeout=57600, future=True)
conn = engine.connect()
df.to_sql("tableName", conn, if_exists='append', index = False)
The full stack trace looks like this:
Traceback (most recent call last):
File "/Users/chent/Desktop/PFSDataParser/src/FetchPFS.py", line 304, in <module>
main()
File "/Users/chent/Desktop/PFSDataParser/src/FetchPFS.py", line 287, in main
insert_to_db(experimentDataSet, expName)
File "/Users/chent/Desktop/PFSDataParser/src/FetchPFS.py", line 89, in insert_to_db
df.to_sql(tableName, conn, if_exists='append', index = False)
File "/Users/chent/opt/anaconda3/lib/python3.9/site-packages/pandas/core/generic.py", line 2951, in to_sql
return sql.to_sql(
File "/Users/chent/opt/anaconda3/lib/python3.9/site-packages/pandas/io/sql.py", line 698, in to_sql
return pandas_sql.to_sql(
File "/Users/chent/opt/anaconda3/lib/python3.9/site-packages/pandas/io/sql.py", line 1754, in to_sql
self.check_case_sensitive(name=name, schema=schema)
File "/Users/chent/opt/anaconda3/lib/python3.9/site-packages/pandas/io/sql.py", line 1647, in check_case_sensitive
with self.connectable.connect() as conn:
AttributeError: 'Connection' object has no attribute 'connect'
The version of pandas I am using is 1.4.4, sqlalchemy is 2.0
I tried to make a several execution of sql query, for example, CREATE TABLE xxx IF NOT EXISTS
or SELECT * FROM
, all of which have given me the result I wish to see.