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

MySQL Database Operations Guide

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)
6 views4 pages

MySQL Database Operations Guide

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 mysql.connector
# Establishing the connection
conn = mysql.connector.connect(host='localhost',
user='root', password='vcplab', database='school')
if conn.is_connected():
print("Connected to MySQL Database")
# Closing the connection
conn.close()

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

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

OUTPUT:
PROGRAM-16
INPUT: Retrieving Data
import mysql.connector
conn=mysql.connector.connect(host='localhost',
user='root', password='vcplab', database='school')
cursor = conn.cursor()
cursor.execute("SELECT * FROM students")
for row in cursor.fetchall():
print(row)
conn.close()

OUTPUT:

You might also like