---Advertisement---

Python with MySQL Connectivity: Complete Guide with Examples for Database & Table Operations Best Guide 2025

By Bhavani

Updated On:

---Advertisement---
Python with MySQL Connectivity

👉Python with MySQL Connectivity

Python with MySQL Connectivity: Integrating Python with MySQL is essential for managing data efficiently in various applications. This guide covers MySQL installation, connector setup, and performing database operations with Python.

👉What is MySQL?

Python with MySQL Connectivity: MySQL is an open-source RDBMS (Relational Database Management System) widely used for handling large data volumes. It was co-founded by Michael Widenius, and its name is derived from his daughter “My.”


👉How to Install MySQL Connector Python on Windows and Linux

👉Install MySQL in Linux/Unix:

  1. Download the RPM package from the official MySQL site: MySQL Downloads
  2. Use the following command in the terminal:

rpm -i <Package_name>

Example: rpm -i MySQL-5.0.9.0.i386.rpm 3. Verify installation using:

mysql –version

👉Python with MySQL Connectivity Install MySQL in Windows:

  1. Download the MySQL database .exe file from the official site.
  2. Follow the standard installation steps.

👉Installing MySQL Connector Library for Python

pip install mysql-connector

pip3 install mysql-connector


👉Testing MySQL Database Connection with Python

To test the database connection, use the connect() method with credentials like host, username, and password.

import mysql.connector

db_connection = mysql.connector.connect(

  host=”localhost”,

  user=”root”,

  passwd=”root”

)

print(db_connection)

👉Output: <mysql.connector.connection.MySQLConnection object at 0x000002338A4C6B00>


👉Creating a Database in MySQL Using Python

CREATE DATABASE “database_name”

import mysql.connector

db_connection = mysql.connector.connect(

  host=”localhost”,

  user=”root”,

  passwd=”root”

)

db_cursor = db_connection.cursor()

db_cursor.execute(“CREATE DATABASE my_first_db”)

db_cursor.execute(“SHOW DATABASES”)

for db in db_cursor:

    print(db)


👉Creating a Table in MySQL with Python

CREATE TABLE student (id INT, name VARCHAR(255))

import mysql.connector

db_connection = mysql.connector.connect(

  host=”localhost”,

  user=”root”,

  passwd=”root”,

  database=”my_first_db”

)

db_cursor = db_connection.cursor()

db_cursor.execute(“CREATE TABLE student (id INT, name VARCHAR(255))”)

db_cursor.execute(“SHOW TABLES”)

for table in db_cursor:

    print(table)

👉Output: (‘student’,)


👉Creating a Table with Primary Key

CREATE TABLE employee(id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), salary INT(6))

import mysql.connector

db_connection = mysql.connector.connect(

  host=”localhost”,

  user=”root”,

  passwd=”root”,

  database=”my_first_db”

)

db_cursor = db_connection.cursor()

db_cursor.execute(“CREATE TABLE employee(id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), salary INT(6))”)

db_cursor.execute(“SHOW TABLES”)

for table in db_cursor:

    print(table)

Output: (’employee’,) (‘student’,)


👉Altering Tables in MySQL with Python

ALTER TABLE student MODIFY id INT PRIMARY KEY

import mysql.connector

db_connection = mysql.connector.connect(

  host=”localhost”,

  user=”root”,

  passwd=”root”,

  database=”my_first_db”

)

db_cursor = db_connection.cursor()

db_cursor.execute(“ALTER TABLE student MODIFY id INT PRIMARY KEY”)


👉Insert Operation with MySQL in Python

INSERT INTO student (id, name) VALUES (01, “John”)

INSERT INTO employee (id, name, salary) VALUES (01, “John”, 10000)

import mysql.connector

db_connection = mysql.connector.connect(

  host=”localhost”,

  user=”root”,

  passwd=”root”,

  database=”my_first_db”

)

db_cursor = db_connection.cursor()

student_sql_query = “INSERT INTO student(id, name) VALUES(01, ‘John’)”

employee_sql_query = “INSERT INTO employee (id, name, salary) VALUES (01, ‘John’, 10000)”

db_cursor.execute(student_sql_query)

db_cursor.execute(employee_sql_query)

db_connection.commit()

print(db_cursor.rowcount, “Record Inserted”)

👉Output: 2 Record Inserted.


Python JSON

Download Python

Leave a Comment

Index