0% found this document useful (0 votes)
8 views

CS Phyton-SQL Connectivity

Uploaded by

Vansh Tomar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

CS Phyton-SQL Connectivity

Uploaded by

Vansh Tomar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Pthyon SQL connectivity

Index

1. Introduction

2. Objective

3. Tools and Technologies Used

4. SQL and Python Connectivity Overview

5. Programs (15 examples)


INTRODUCTION

This project demonstrates various programs to perform


database operations by integrating Python with SQL using
the MySQL Connector.

OBJECTIVE
To understand how to connect Python with SQL.

To perform various CRUD operations using Python scripts.

TOOLS AND TECHNOLOGIES USED

Python: For scripting and executing queries.

MySQL: As the database management system.

MySQL Connector Module: For integrating Python and


MySQL.
PYTHON-SQL CONNECTIVITY
OVERVIEW

1. Install MySQL Connector:

pip install mysql-connector-python

2. Establish a connection:

import mysql.connector
conn = mysql.connector.connect(
host="localhost",
user="root",
password="password",
database="testdb"
)
cursor = conn.cursor()
PROGRAMS
Program 1: Connect to the Database
import mysql.connector

conn = mysql.connector.connect(
host="localhost",
user="root",
password="password"
)
if conn.is_connected():
print("Connected to MySQL!")

Program 2: Create a Database

cursor.execute("CREATE DATABASE sampledb")


print("Database created!")

Program 3: Create a Table


query = "INSERT INTO students (name, age) VALUES (%s, %s)"
data = [("John", 20), ("Alice", 22), ("Bob", 19)]
cursor.executemany(query, data)
conn.commit()
print("Data inserted!")
Program 4: Insert Data into the Table
query = "INSERT INTO students (name, age) VALUES (%s, %s)"
data = [("John", 20), ("Alice", 22), ("Bob", 19)]
cursor.executemany(query, data)
conn.commit()
print("Data inserted!")

Program 5: Fetch All Records


cursor.execute("SELECT * FROM students")
rows = cursor.fetchall()
for row in rows:
print(row)

Program 6: Fetch a Specific Record


query = "SELECT * FROM students WHERE name = %s"
cursor.execute(query, ("Alice",))
print(cursor.fetchone())

Program 7: Update a Record


query = "UPDATE students SET age = %s WHERE name = %s"
cursor.execute(query, (23, "John"))
conn.commit()
print("Record updated!")
Program 8: Delete a Record
query = "DELETE FROM students WHERE name = %s"
cursor.execute(query, ("Bob",))
conn.commit()
print("Record deleted!
")

Program 9: Drop a Table

cursor.execute("DROP TABLE students")


print("Table dropped!")

Program 10: Show All Databases

cursor.execute("SHOW DATABASES")
for db in cursor:
print(db)

Program 11: Use a Specific Database

cursor.execute("USE sampledb")
print("Database switched!")
Program 12: Add a New Column

cursor.execute("ALTER TABLE students ADD COLUMN gender


VARCHAR(10)")
print("Column added!")

Program 13: Count Records in a Table

cursor.execute("SELECT COUNT(*) FROM students")


print("Total records:", cursor.fetchone()[0])

Program 14: Sort Records

cursor.execute("SELECT * FROM students ORDER BY age DESC")


for row in cursor.fetchall():
print(row)

Program 15: Execute a Custom Query

query = "SELECT name, age FROM students WHERE age > 20"
cursor.execute(query)
for row in cursor.fetchall():
print(row)
CONCLUSION
This project highlights the
fundamentals of Python and SQL
integration, demonstrating essential
database operations such as creating,
updating, and retrieving records.

You might also like