how to export data between pandas and Mysql code example

Example 1: mysql to df

import mysql.connector
import pandas as pd

db_connection = mysql.connector.connect(
  host="localhost",
  user="user",
  password="password",
  database="dbname",
  auth_plugin='mysql_native_password'
)

df = pd.read_sql("select * from table_name;", con=db_connection)
print(df)

Example 2: working with mysql database in pandas

Importing data from a MySQL database into a Pandas data frame including column names:-
import mysql.connector as sql.
import pandas as pd.
db_connection = sql.connect(host='hostname', database='db_name', user='username', password='password')
db_cursor = db_connection.cursor()
db_cursor.execute('SELECT * FROM table_name')

Tags:

Sql Example