first try to install mysql connector using this- pip install mysql-connector-python
after that use import mysql.connector
to import the connector
Then connect your database using this-
myconn = mysql.connector.Connect(user='root',password="1234567890",database="world",auth_plugin='mysql_native_password')
where password is your mysql password and database is your name of database you want to connect
suppose you want to fetch all rows from your selected table-
myconn = mysql.connector.Connect(user='root',password="1234567890",database="world")
cur = myconn.cursor()
cur.execute("Select * from emptab")
allrows = cur.fetchall()
print(allows)
this would generate result like that-
(1001, 'RamKumar', 10000)
(1002, 'Ganesh Kumar', 1000)
(1003, 'Rohan', 3450)
(1004, 'Harish Kumar', 56000)
(1005, 'Mohit', 12000)
(1006, 'Harish Nagar', 56000)
to convert above data to CSV form first convert it into an pandas data frame-
df = pd.DataFrame(allrows)
df.columns = ['empno','name','salary']
this would convert data into pandas df and with columns above
at last use
df_final.to_csv(r'final.csv', index = False)
like command to save result to csv form