-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmydbhelper.py
61 lines (49 loc) · 1.76 KB
/
mydbhelper.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
import pymysql
def connect():
return pymysql.connect(
host="localhost",
user="vinayak",
port=3306,
password="3553",
database="test",
cursorclass=pymysql.cursors.DictCursor,
)
def getAllStudents():
connection = connect()
cursor = connection.cursor()
cursor.execute("SELECT * FROM students;")
rows = cursor.fetchall()
cursor.close()
connection.close()
return rows
def getStudentRecord(rollNumber):
connection = connect()
cursor = connection.cursor()
command_to_execute = f"SELECT * FROM students WHERE ROLL={rollNumber};"
cursor.execute(command_to_execute)
rows = cursor.fetchone()
cursor.close()
connection.close()
return rows
def insertRecord(roll, name, gender, contact, dob, address):
# Auto-generated SQL script #202108191721
connection = connect()
cursor = connection.cursor()
command_to_execute = f"INSERT INTO test.students (ROLL,NAME,GENDER,CONTACT,DOB,ADDRESS) VALUES ({int(roll)},'{name}','{gender}','{contact}','{dob}','{address}');"
cursor.execute(command_to_execute)
connection.commit()
connection.close()
def removeRecord(rollNumber):
connection = connect()
cursor = connection.cursor()
command_to_execute = f"DELETE FROM test.students WHERE ROLL={rollNumber.get()};"
cursor.execute(command_to_execute)
connection.commit()
connection.close()
def updateRecord(roll, name, gender, contact, dob, address):
connection = connect()
cursor = connection.cursor()
command_to_execute = f"UPDATE test.students SET NAME='{name}',GENDER='{gender}',CONTACT='{contact}',DOB='{dob}',ADDRESS='{address}' WHERE ROLL={roll};"
cursor.execute(command_to_execute)
connection.commit()
connection.close()