Python MQL Connectivity Programs
Python MQL Connectivity Programs
SOURCE CODE:
import mysql.connector
# Establish a connection to the MySQL server
mycon = mysql.connector.connect(
host='localhost',
user='root',
password='password',
database='School')
if mycon.is_connected():
print('Database connected')
# Create a cursor object
cursor = mycon.cursor()
# Create the table
sql = '''
CREATE TABLE IF NOT EXISTS STUDENT (
ID INT PRIMARY KEY,
Name CHAR(10),
Stream CHAR(10),
AvgMark FLOAT,
Grade CHAR(2),
Class CHAR(5))'''
cursor.execute(sql)
# Insert records
sql = '''
INSERT INTO STUDENT (ID, Name, Stream, AvgMark, Grade, Class)
VALUES
(1, 'Karan', 'Medical', 78.5, 'B', '128'),
(2, 'Divakar', 'Commerce', 89.2, 'A', '11C'),
(3, 'Divya', 'Commerce', 68.6, 'C', '12C'),
(4, 'Arun', 'Computers', 89.9, 'A', '12A'),
(5, 'Sabina', 'Medical', 83.3, 'B', '128') '''
cursor.execute(sql)
OUTPUT:
PROGRAM 2: Write a program to connect python with mysql using database
connectivity and perform the following operations in database:
SOURCE CODE:
import mysql.connector
if mycon.is_connected():
print('Database connected')
OUTPUT:
PROGRAM 3: Write a program to connect python with mysql using database connectivity and
perform the following operations in database:
a) Display all the records of students in the ascending order of Name of the student
SOURCE CODE:
import mysql.connector
if mycon.is_connected():
print('Database connected')
OUTPUT:
SOURCE CODE:
import mysql.connector
OUTPUT: