WORKING WITH MYSQL DATABASE AND PYTHON
This is a simple program on connecting your python with database and then working with simple SQL query. If you are using Pycharm IDE then you need to first install
mysql connector for your python program to work with MySQL database. For this you can go to File->Settings->Project:(you will see your project name)->Python Interpreter. Then click
+ sign in the top right and search for mysql-connector. Then you can click that and install the package. Then you are good to go.
I had a database name called
userdb in MySQL Workbench and
student is the table of that database. I am printing whatever is inside that table. This query will show that.
import mysql.connector mydb=mysql.connector.connect(host="localhost", user="root", passwd="12345", database="userdb") mycursor=mydb.cursor() mycursor.execute("select * from student") for i in mycursor: print(i)
As you run this program, the following output will be seen.