Skip to content

Instantly share code, notes, and snippets.

@Swalloow
Created March 11, 2017 18:40
Show Gist options
  • Save Swalloow/9dbbba9579b87548c71af68337f95299 to your computer and use it in GitHub Desktop.
Save Swalloow/9dbbba9579b87548c71af68337f95299 to your computer and use it in GitHub Desktop.
DataFrame to MySQL
def save_db(df, table):
try:
engine = create_engine("mysql+mysqldb://root:"+"password"+"@localhost/"+table, encoding='utf-8')
conn = engine.connect()
# Save dataframe to database
df.to_sql(name=table, con=engine, if_exists='append')
print("Saved successfully!!")
except:
traceback.print_exc()
finally:
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment