I’m having problems with the connector engine from SQLAlchemy connecting to MySQL.
The database and table I want to insert into are already created in MySQL.
I keep getting an AttributeError: ‘Engine’ object has no attribute ‘cursor’
I have the web scraped data into a dataframe and I’m trying to send it into MySQL.
And the error occurs when I run:
df.to_sql(‘ibm_stock_webdata’, con=engine, if_exists=‘replace’, index = True)
I saved all credentials in an .env file:
load_dotenv()
api_key = os.getenv("API_KEY")
mysql_user = os.getenv("MYSQL_USER")
mysql_password = os.getenv("MYSQL_PASSWORD")
mysql_host = os.getenv("MYSQL_HOST")
mysql_database = os.getenv("MYSQL_DATABASE")
if not api_key:
raise ValueError("API key not found. Make sure your .env file is set up correctly")
if not all([mysql_user, mysql_password, mysql_host, mysql_database]):
raise ValueError("MySQL connection details not found. Make sure your .env file is set up correctly.")
Created a connector string:
mysql_connection_string = f'mysql+pymysql://{mysql_user}:{mysql_password}@{mysql_host}/{mysql_database}'
Created the engine:
engine = create_engine(mysql_connection_string)
The error occurs in this code block:
stock_data.to_sql('ibm_stock_webdata', con=engine, index=True, if_exists='replace')
[Code Block With Errors](https://i.sstatic.net/MBHaGFVp.png)
Bryan Hawkshaw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.