-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDatabase_Connectivity.py
More file actions
152 lines (132 loc) · 4.98 KB
/
Database_Connectivity.py
File metadata and controls
152 lines (132 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/python3
# -----------------------------------------------------------------------------
# Script Name: Database_Connectivity.py
# Experiment: Experiment 7, Program 7.1
# Description: Demonstrates Python connectivity with MySQL database.
# Includes CRUD (Create, Read, Update, Delete) operations.
#
# Authors: Amey Thakur
# Repository: https://github.com/Amey-Thakur/OPEN-SOURCE-TECH-LAB
# License: CC BY 4.0
# -----------------------------------------------------------------------------
import mysql.connector
from mysql.connector import Error
def insert_record(cursor, conn):
"""
Function to insert a new student record into the database.
"""
try:
print("\n--- Insert New Student Record ---")
r = int(input('Enter Roll No: '))
n = input('Enter Full Name: ')
m = input('Enter Mobile No: ')
a = input('Enter Address: ')
c = input('Enter Courses: ')
cr = int(input('Enter Credits: '))
sk = input('Enter Skills: ')
co = int(input('Enter No. of Courses: '))
query = "INSERT INTO Student (rollno, name, mob, address, courses, no_of_credits, skills, no_of_courses) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"
values = (r, n, m, a, c, cr, sk, co)
cursor.execute(query, values)
conn.commit()
print('>> Record inserted successfully!')
except ValueError:
print('Error: Invalid numerical input.')
except Error as e:
print(f'Error: Database insertion failed: {e}')
def display_records(cursor):
"""
Function to display all records from the Student table.
"""
print("\n--- Displaying All Records ---")
try:
query = 'SELECT * FROM Student'
cursor.execute(query)
records = cursor.fetchall()
if not records:
print("No records found in the database.")
return
for row in records:
print(row)
except Error as e:
print(f'Error: Could not retrieve records: {e}')
def update_record(cursor, conn):
"""
Function to update an existing record by Roll No.
"""
print("\n--- Update Existing Record ---")
try:
r = int(input('Enter Roll No to update: '))
n = input('Enter New Name: ')
query = "UPDATE Student SET name = %s WHERE rollno = %s"
cursor.execute(query, (n, r))
conn.commit()
if cursor.rowcount > 0:
print(f'>> Record for Roll No {r} updated successfully!')
else:
print(f'>> No record found with Roll No {r}.')
except Error as e:
print(f'Error: Update failed: {e}')
def delete_record(cursor, conn):
"""
Function to delete a student record by Roll No.
"""
print("\n--- Delete Student Record ---")
try:
r = int(input('Enter Roll No to delete: '))
query = "DELETE FROM Student WHERE rollno = %s"
cursor.execute(query, (r,))
conn.commit()
if cursor.rowcount > 0:
print(f'>> Record for Roll No {r} deleted successfully!')
else:
print(f'>> No record found with Roll No {r}.')
except Error as e:
print(f'Error: Deletion failed: {e}')
def main():
"""
Main driver for Database Connectivity demo.
"""
try:
# Configuration for local MySQL server
conn = mysql.connector.connect(
host="localhost",
user="root", # Updated to common default
passwd="", # Blank by default on many setups
database="ostldb" # Assuming ostldb exists
)
if conn.is_connected():
cursor = conn.cursor()
while True:
print("\n" + "=" * 40)
print("\tDATABASE OPERATIONS MENU")
print("=" * 40)
print("1. INSERT RECORD")
print("2. DISPLAY ALL RECORDS")
print("3. UPDATE RECORD (NAME)")
print("4. DELETE RECORD")
print("5. EXIT")
print("=" * 40)
try:
ch = int(input('Enter your choice: '))
if ch == 5:
break
elif ch == 1:
insert_record(cursor, conn)
elif ch == 2:
display_records(cursor)
elif ch == 3:
update_record(cursor, conn)
elif ch == 4:
delete_record(cursor, conn)
else:
print('Error: Invalid choice.')
except ValueError:
print('Error: Please enter a number.')
cursor.close()
conn.close()
print("\nDatabase connection closed. Goodbye!")
except Error as e:
print(f"Error: Could not connect to MySQL server: {e}")
if __name__ == '__main__':
main()