In the previous article, we learned how to fetch MySQL table data in Python. Now, we will read/fetch MySQL table in Python using Panda. The following code will connect to an existing MySQL table and fetch the data.
#Create a new MySQL Connection
import mysql.connector
import pandas as pd
try:
conn = mysql.connector.connect(host='192.168.100.11',
user='dhani',
password='Test.0124',
database='test')
#Create a new query
myquery = 'select * from Tbl_DHCollar'
#Create a new dataframe and load the data into dataframe
mydataframe = pd.read_sql(myquery,conn)
print(mydataframe)
#Catch the error
except mysql.connector.Error as e:
print(r"Error: {e}")
In the previous article, we did not use dataframe and the result is not tidy. But now we use the dataframe and it reads the table headers and it looks better.