Python MySQL Tutorial : Using MySQL Database with Python

Do you want to use MySQL database in your Python Code? And struggling to make it work? Then this is the right place, in this Python MySQL Tutorial, we will learn using MySQL database in Python. We have a pure python library called PyMySQL, and we can use it to work with MySQL database. Now let’s see how we do this.

What is PyMySQL

You might already know that to use any database we need the database driver. And PyMySQL is a pure python driver for operating MySQL in Python.
Now, remember, this PyMySQL is not available by default. So, first, we will learn how do we download and install this driver.

Installing PyMySQL

Here, I am assuming you have python installed already on your machine. And also you have done with the setting environment variables stuff. If not then you should go to this link first.

  • To install PyMySQL run the following command, (I am using windows, so I am using cmd, for MAC of Linux use terminal).

  • Make sure you are connected with internet and you will see the following output on running the above command.

python mysql tutorial

Now, once we have the PyMySQL installed, we can learn operating MySQL database from Python. In this Python MySQL Tutorial, I will tell you the basic CRUD operations in MySQL database using Python.

Note: CRUD means the Create, Read, Update and Delete operation in a Database.

Creating Database in MySQL

Before starting any operation, we need a database, right? So open your MySQL database, here for MySQL I am using XAMPP, and I can access it with localhost/phpmyadmin.
Though you can use any other software as well, the process will be the same.

  • Go to localhost/phpmyadminย and create a database. I have created a database named belal, as you can see in the below image.

mysql database

  • Now, inside the database we will create the table. So go to SQL and run the following SQL query to create your table.

  • The above query will create the following table.

mysql table

Python MySQL Tutorial : Performing Basic CRUD

Here, I am using the PyCharm IDE. (I am in love with JetBrains IDEs ๐Ÿ˜› ).

Now, let’s start with a new Python Project in PyCharm.

Create Operation

  • Create a new Python File named CreatTask.py in your project. The first step is importing pymysql.

  • Then we will connect to the database as.

  • Now we will get the user inputs, as we need to store title, description and date in the table. So we will get these values from the user.

  • So, we have the values, and the connection to the MySQL database. To insert the values we do it as.

  • The final code we have for AddTask.py is.

  • You can try executing this code.
python mysql tutorial
Python MySQL Tutorial
  • If you are getting the output as shown above, you should see the values in your MySQL database.

mysql table values

Read Operation

  • Again create a new Python file named ReadTasks.py and write the following code in it.

  • On running the above code you will see the following output.

python mysql tutorial

Update Operation

I hope now; you can perform all operations by yourself. But still, if you have any confusions here is the code snippet for updating values in MySQL Database.

Delete Operation

  • Finally you can delete a record as.

And we have done with all the CRUD operations in MySQL. So that’s all for this Python MySQL Tutorial friends. I hope you found it helpful if you did, then please SHARE this post with your friends who are learning python.

And yes, if you have any query regarding this Python MySQL Tutorial then your questions are welcome. Thank You ๐Ÿ™‚

1 thought on “Python MySQL Tutorial : Using MySQL Database with Python”

Leave a Comment