Skip to content

Instantly share code, notes, and snippets.

@MinaGabriel
Created March 16, 2020 16:51
Show Gist options
  • Save MinaGabriel/a2915282bd6294a0a99ff57cb107838d to your computer and use it in GitHub Desktop.
Save MinaGabriel/a2915282bd6294a0a99ff57cb107838d to your computer and use it in GitHub Desktop.
import os
import pandas as pd
import mysql.connector
from sqlalchemy import create_engine
# ====== Connection ====== #
# Connecting to mysql by providing a sqlachemy engine
engine = create_engine('mysql+mysqlconnector://myhu:password@192.168.68.102/dbname', echo=True)
list_hello = ['hello1', 'hello2']
list_world = ['world1', 'world2']
df = pd.DataFrame(data={'hello': list_hello, 'world': list_world})
# engine.execute("CREATE DATABASE dbname") #create db
print(engine.table_names())
df.to_sql(name='helloworld', con=engine, if_exists='replace')
print(df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment