Skip to main content

Writing Data to a Database using Python

This article will go over how to write data to a MySQL database. This method can be altered by changing the SQLAlchemy URI and database connector. You can read more about SQLAlchemy here.


pip3 install requests pandas mysql-connector
  1. Go to the Integrate tab for your extractor
  2. Copy the CSV from the last successful run endpoint
  3. Replace the endpoint into the below with the CSV endpoint
import requests  
import pandas as pd
import mysql.connector
from sqlalchemy import create_engine


session = requests.Session()
response = session.get(url=API_ENDPOINT).content

df = pd.read_csv(io.StringIO(response.decode('utf-8-sig')))


engine = create_engine('mysql+mysqlconnector://os.environ['MYSQL_USER']:os.environ['MYSQL_PASSWORD']@os.environ['MYSQL_HOST_IP']:os.environ['MYSQL_PORT']/sandbox', echo=False)

# Writing Dataframe to Mysql and replacing table if it already exists
df.to_sql(name='Importio', con=engine, if_exists = 'replace', index=False)