2

I intend to export a pandas dataframe to MySQL using SQLAlchemy. Despite referring to all previous posts, I am unable to solve the issue:

import pandas as pd
import pymysql
from sqlalchemy import create_engine

df=pd.read_excel(r"C:\Users\mazin\1-601.xlsx")

cnx = create_engine('mysql+pymysql://[root]:[aUtO1115]@[localhost]:[3306]/[patenting in psis]', echo=False)

df.to_sql(name='inventor_dataset', con=cnx, if_exists = 'replace', index=False)

Following is the error:

OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'localhost]:[3306' ([Errno 11001] getaddrinfo failed)")

3
  • are you able to connect to the sql database via cmd/terminal? Commented Feb 8, 2018 at 16:12
  • @iam.Carrot - I am using Python to interact with MySQL for the first time. I was able to input data from a sample MySQL table in Python as a pandas dataframe. Commented Feb 8, 2018 at 16:24
  • It seems that the database is not running. Please ensure your MySQL database is started, then use a client like MySQL Workbench to try to connect. Commented Jun 23, 2023 at 0:52

2 Answers 2

2

After a lot of tinkering with the code and exploring different packages in Python, I was able to make the code work.

Code:

import mysql.connector
import sqlalchemy

database_username = 'root'
database_password = 'mysql'
database_ip       = '127.0.0.1'
database_name     = 'patenting_in_psi'
database_connection = sqlalchemy.create_engine('mysql+mysqlconnector://{0}:{1}@{2}/{3}'.
                                               format(database_username, database_password, 
                                                      database_ip, database_name), pool_recycle=1, pool_timeout=57600).connect()

df22.to_sql(con=database_connection, name='university_dataset_ca', if_exists='append',chunksize=100)
database_connection.close()
Sign up to request clarification or add additional context in comments.

Comments

0

Here is another option:

import pandas as pd
import sqlalchemy

# Read your CSV file into a Pandas dataframe
df = pd.read_csv("test.csv")

# Store the dataframe in MySQL
engine = sqlalchemy.create_engine('mysql+mysqlconnector://user:[email protected]/schema')

with engine.begin() as connection:
    df.to_sql('your_table', con=connection, if_exists='replace', index=False)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.