Python Tutorial – Working with MySQL Database in Python

In this Python Tutorial we are going to learn Working with MySQL Database in Python, if you want to connect your Python code with MySQL Database than you need to use third party library, there are different libraries that you can use, in this Python Tutorial we want to use Mysql Connector. as you know MySQL is an open-source relational database management system. Its name is a combination of “My”, the name of co-founder Michael Widenius’s daughter, and “SQL”, the abbreviation for Structured Query Language.

 

 

 

 

Installation 

First of all you need to install mysql connector, you can simply use pip for the installation.

 

 

 

Also you need to download and install Wamp Server, because we want to use Wamp Server as virtual server.

 

 

1: Creating MySQL Database in Python

So first we want to create a database using Python code, so this is the code for creating mysql database in Python.

 

 

 

First of all you need to import mysql connector for python, after that connect your code with the wamp server like this. give the host and user name, also give the password if you  have.

 

 

 

In here we want to get the database name from the user or we can say from the input, and according to that input we want to create our database in mysql.

 

 

For executing the query you need to create the object of the cursor.

 

 

 

At the end we need to execute our database creation query.

 

 

 

Run the code give the name of the database that you want.

Python Tutorial - Working with MySQL Database in Python
Python Tutorial – Working with MySQL Database in Python

 

 

 

 

Check your Wamp Server we have the database.

Python Mysql Database Wamp Server
Python Mysql Database Wamp Server

 

 

 

 

2: Checking MySQL Database Connection in Python

After creating of the database, now we want to check the database connection, you can use this code for checking mysql database connection in python.

 

 

 

 

Right now we have the correct database name and if we run the code, you will see the connection.

Python Database Connection
Python Database Connection

 

 

 

 

 

3: Insert Data to Mysql Database in Python

Now we want to insert some data in our MySQL database, first of all you need to create a table in your pythondb database, iam going to call my table name users. and add three fields id,email and password. and this is the code for inserting the data.

 

 

 

 

We want to insert data according to the user input.

 

 

 

 

This is our query for inserting the data.

 

 

 

Also you need to execute your query and commit the database.

 

 

 

 

Run the code give email and password, this will be the result.

Python Tutorial - Working with MySQL Database in Python
Python Tutorial – Working with MySQL Database in Python

 

 

 

 

Check Wamp Server and you have the data.

Python Mysql Wamp Server
Python Mysql Wamp Server

 

 

 

 

4: Select Data From Mysql Database in Python

In this part we want to select or retrieve our data from mysql database in Python. so this is the code for selecting the data.

 

 

 

 

We want to select the data according to user input by entering database name and table name.

 

 

 

This is the query for selecting the data. 

 

 

 

Also we need to fetch all the data from the cursor object using fetchall() method.

 

 

 

Iterate over the data.

 

 

 

 

Run the code and this is the result.

Working with MySQL Database in Python
Working with MySQL Database in Python

 

 

 

 

5: Updating Data From Mysql Database in Python

For updating the data you can use this code, just we have connected our python code to the Wamp Server using mysql connector and after that we create the object of cursor and execute the query.

 

 

 

 

 

Run the code and this is the result.

Python Mysql Updating Data
Python Mysql Updating Data

 

 

 

 

6: Deleting Data From Mysql Database in Python

This code is for deleting the data in Python Mysql.

 

 

 

 

 

Also you can watch the complete vide for this article 

 

 

 

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Share via
Copy link
Powered by Social Snap
×