Class XII - Python MySQL (PyMySQL) Practical
Programs
1. Connect Python with MySQL
import pymysql
con = [Link](host="localhost", user="root", password="your_password", database="school")
print("Connection Successful!")
[Link]()
Output: Connection Successful!
2. Create Table
cur = [Link]()
[Link]("CREATE TABLE student (roll INT, name VARCHAR(30), marks INT)")
print("Table Created Successfully!")
Output: Table Created Successfully!
3. Insert Data
[Link]("INSERT INTO student VALUES (1, 'Rahul', 85)")
[Link]()
print("Record Inserted!")
Output: Record Inserted!
4. Display Records
[Link]("SELECT * FROM student")
rows = [Link]()
for r in rows:
print(r)
Output: (1, 'Rahul', 85)
5. Update Record
[Link]("UPDATE student SET marks=95 WHERE roll=1")
[Link]()
print("Record Updated!")
Output: Record Updated!
6. Delete Record
[Link]("DELETE FROM student WHERE roll=1")
[Link]()
print("Record Deleted!")
Output: Record Deleted!
7. Search Record
roll = int(input("Enter roll: "))
[Link]("SELECT * FROM student WHERE roll=%s", (roll,))
data = [Link]()
print(data)
Output: (2, 'Anita', 90)