how to read mysql table in python code example

Example 1: how to read mysql table in python

# for those who can't see all columns in the
# output when getting data from database

# just paste in the following code and that should be fixed
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
pd.set_option('display.max_colwidth', None)

Example 2: how to read mysql table in python

import pandas as pd
import sqlalchemy

# make sure you have installed both mysql & pymysql

													# root:root is my username and password/default
engine = sqlalchemy.create_engine("mysql+pymysql://root:root@localhost/database name", pool_pre_ping=True)
# then specify the name of the table in the database
df = pd.read_sql_table('table name', engine)
print(df)

Example 3: python retrieves records after db select query

cursor.fetchall()

Tags:

Misc Example