-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.py
141 lines (125 loc) · 4.62 KB
/
Database.py
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
import mysql.connector
#make sure you written everthing write
#install mysql.connector through(pip install mysql-connector-python)
#its here till we have new project help me out in that
#call me out if to make change in this file
conn = mysql.connector.connect(
host='localhost',
user='root',
passwd='root',
port=3306,
database='your_databse'
)
cursor = conn.cursor()
#its worked on my databse
def read_data(cursor, conn):
query = "SELECT * FROM student;"
cursor.execute(query)
table = cursor.fetchall()
for row in table:
print(row)
#change the values according to your table and databse
def add_new_details(cursor, conn):
try:
rno = int(input("Enter Roll No. of New student ...Check in Database before inserting new details: "))
sname = input("Enter student name: ")
coursename = input("Enter course name: ") # New column
query = f"INSERT INTO student VALUES ({rno}, '{sname}', '{coursename}');"
cursor.execute(query)
conn.commit()
print("Inserted successfully!")
except Exception as e:
conn.rollback()
print("Error inserting data:", e)
def update_details(cursor, conn):
try:
inn = input("What do you want to update ('rno', 'sname', or 'course_name'): ")
if inn == 'rno':
print("IN RNO UPDATION")
give = input("Give the WHERE condition: ")
rn = input("Enter the new RNO: ")
query = f"UPDATE student SET rno = {rn} WHERE sname = '{give}';"
cursor.execute(query)
conn.commit()
print("UPDATED")
elif inn == 'sname':
print("IN SNAME UPDATION")
where = input("Give the WHERE condition: ")
sname = input("Enter the new SNAME: ")
query = f"UPDATE student SET sname = '{sname}' WHERE rno = {where};"
cursor.execute(query)
conn.commit()
print("UPDATED")
elif inn == 'course_name': # New column
print("IN COURSENAME UPDATION")
where = input("Give the WHERE condition: ")
coursename = input("Enter the new COURSENAME: ")
query = f"UPDATE student SET course_name = '{coursename}' WHERE rno = {where};"
cursor.execute(query)
conn.commit()
print("UPDATED")
else:
print("Invalid option. Please try again.")
except Exception as e:
conn.rollback()
print("An error occurred:", e)
def add_column(cursor, conn):
try:
column_name = input("Enter the name of the new column: ")
data_type = input("Enter the data type of the new column: ")
query = f"ALTER TABLE student ADD COLUMN {column_name} {data_type};"
cursor.execute(query)
conn.commit()
print("Column added successfully!")
except Exception as e:
conn.rollback()
print("Error adding column:", e)
def delete_user(cursor, conn):
try:
rno = int(input("Enter the Roll No. of the student to delete: "))
query = f"DELETE FROM student WHERE rno = {rno};"
cursor.execute(query)
conn.commit()
print("User deleted successfully!")
except Exception as e:
conn.rollback()
print("Error deleting user:", e)
def delete_column(cursor, conn):
try:
column_name = input("Enter the name of the column to delete: ")
query = f"ALTER TABLE student DROP COLUMN {column_name};"
cursor.execute(query)
conn.commit()
print("Column deleted successfully!")
except Exception as e:
conn.rollback()
print("Error deleting column:", e)
#ruining in while loop until the statement is wrong
while True:
print("\n1. Read Details in Database")
print("2. Insert Details in Database")
print("3. Update a detail in Database")
print("4. Add a new column")
print("5. Delete a user")
print("6. Delete a column")
print("7. Exit")
choice = input("Enter your choice (1-7): ")
if choice == '1':
read_data(cursor, conn)
elif choice == '2':
add_new_details(cursor, conn)
elif choice == '3':
update_details(cursor, conn)
elif choice == '4':
add_column(cursor, conn)
elif choice == '5':
delete_user(cursor, conn)
elif choice == '6':
delete_column(cursor, conn)
elif choice == '7':
print("Exiting...")
break
else:
print("Invalid choice. Please enter a number between 1-7")
cursor.close()
conn.close()