Before diving deep, let’s understand
What is MySQL?
MySQL is an Open-Source database and one of the best type of RDBMS (Relational Database Management System). Co-founder of MySQLdb is Michael Widenius’s, and also MySQL name derives from the daughter of Michael. In this tutorial, you will learn
What is MySQL? How to Install MySQL Install MySQL Connector Library for Python Test the MySQL Database connection with Python Creating Database in MySQL using Python Create a Table in MySQL with Python Create a Table with Primary Key ALTER table in MySQL with Python Insert Operation with MySQL in Python
How to Install MySQL Connector Python on Windows, Linux/Unix
Install MySQL in Linux/Unix:
Download RPM package for Linux/Unix from Official site: https://www.mysql.com/downloads/ In terminal use following command To check in Linux
Install MySQL in Windows
Download MySQL database exe from official site and install as usual normal installation of software in Windows. Refer this tutorial, for a step by step guide
How to Install MySQL Connector Library for Python
Here is how to connect MySQL with Python: For Python 2.7 or lower install using pip as: For Python 3 or higher version install using pip3 as:
Test the MySQL Database connection with Python
To test MySQL database connectivity in Python here, we will use pre-installed MySQL connector and pass credentials into connect() function like host, username and password as shown in the below Python MySQL connector example. Syntax to access MySQL with Python: Example: Output: Here output shows the connection created successfully.
Creating Database in MySQL using Python
Syntax to Create new database in SQL is Now we create database using database programming in Python Output:
Here above image shows the my_first_db database is created
Create a Table in MySQL with Python
Let’s create a simple table “student” which has two columns as shown in the below MySQL connector Python example. SQL Syntax: Example: Output:
Create a Table with Primary Key
Let’s create an Employee table with three different columns. We will add a primary key in id column with AUTO_INCREMENT constraint as shown in the below Python project with database connectivity. SQL Syntax: Example: Output:
ALTER table in MySQL with Python
Alter command is used for modification of Table structure in SQL. Here we will alter Student table and add a primary key to the id field as shown in the below Python MySQL connector project. SQL Syntax: Example: Output: Here below you can see the id column is modified.
Insert Operation with MySQL in Python:
Let’s perform insertion operation in MySQL Database table which we already create. We will insert data oi STUDENT table and EMPLOYEE table. SQL Syntax: Example: Output: Also Check:- Python Tutorial for Beginners: Learn Programming Basics [PDF]