Skip to content

Instantly share code, notes, and snippets.

@dei-biz
Created May 14, 2018 17:19
Show Gist options
  • Select an option

  • Save dei-biz/78fd11b0355dd3e43942242892760aa3 to your computer and use it in GitHub Desktop.

Select an option

Save dei-biz/78fd11b0355dd3e43942242892760aa3 to your computer and use it in GitHub Desktop.
Python3 XLSX from MySQL
import pandas as pd
host = 'HOST'
port = 3306
user = 'USER'
password = 'PASS'
database = 'DATABASE'
writer = pd.ExcelWriter(database+'.xlsx')
conn = pymysql.connect(
host=host,
port=int(port),
user=user,
passwd=password,
db=database,
charset='latin1')
tables = pd.read_sql_query("SELECT TABLE_NAME AS t FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '"+database+"'",
conn)
for index, row in tables.iterrows():
df = pd.read_sql_query("SELECT * from "+row.t, conn)
df.to_excel(writer, row.t)
writer.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment