0% found this document useful (0 votes)
4 views4 pages

MySQL Database Connection and Operations

Uploaded by

eklavya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

MySQL Database Connection and Operations

Uploaded by

eklavya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PROGRAM-13

INPUT: Connecting to MySQL


import [Link]
# Establishing the connection
conn = [Link](host='localhost',
user='root', password='vcplab', database='school')
if conn.is_connected():
print("Connected to MySQL Database")
# Closing the connection
[Link]()

OUTPUT:
PROGRAM-14
INPUT: Creating a Table
Creating a Table
import [Link] conn =
[Link](host='localhost', user='root',
password='vcplab', database='school')
cursor = [Link]()
[Link]("CREATE TABLE IF NOT EXISTS students (id
INT PRIMARY KEY, name VARCHAR(50), age INT)")
print("Table 'students' created successfully")
[Link]()

OUTPUT:
PROGRAM-15
INPUT: Inserting Data
import [Link]
conn = [Link](host='localhost',
user='root', password='vcplab', database='school')
cursor = [Link]() query = "INSERT INTO students (id,
name, age) VALUES (%s, %s, %s)"
values = (1, 'John Doe', 18)
[Link](query, values)
[Link]()
print("Data inserted successfully")
[Link]()

OUTPUT:
PROGRAM-16
INPUT: Retrieving Data
import [Link]
conn=[Link](host='localhost',
user='root', password='vcplab', database='school')
cursor = [Link]()
[Link]("SELECT * FROM students")
for row in [Link]():
print(row)
[Link]()

OUTPUT:

You might also like