-
Notifications
You must be signed in to change notification settings - Fork 13
/
dbHandler.py
64 lines (50 loc) · 1.72 KB
/
dbHandler.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
import pymysql
def insertData(data):
rowId = 0
db = pymysql.connect("localhost", "criminaluser", "", "criminaldb")
cursor = db.cursor()
print("database connected")
query = "INSERT INTO criminaldata VALUES(0, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');" % \
(data["Name"], data["Father's Name"], data["Mother's Name"], data["Gender"],
data["DOB(yyyy-mm-dd)"], data["Blood Group"], data["Identification Mark"],
data["Nationality"], data["Religion"], data["Crimes Done"])
try:
cursor.execute(query)
db.commit()
rowId = cursor.lastrowid
print("data stored on row %d" % rowId)
except:
db.rollback()
print("Data insertion failed")
db.close()
print("connection closed")
return rowId
def retrieveData(name):
id = None
crim_data = None
db = pymysql.connect("localhost", "criminaluser", "", "criminaldb")
cursor = db.cursor()
print("database connected")
query = "SELECT * FROM criminaldata WHERE name='%s'"%name
try:
cursor.execute(query)
result = cursor.fetchone()
id=result[0]
crim_data = {
"Name" : result[1],
"Father's Name" : result[2],
"Mother's Name" : result[3],
"Gender" : result[4],
"DOB(yyyy-mm-dd)" : result[5],
"Blood Group" : result[6],
"Identification Mark" : result[7],
"Nationality" : result[8],
"Religion" : result[9],
"Crimes Done" : result[10]
}
print("data retrieved")
except:
print("Error: Unable to fetch data")
db.close()
print("connection closed")
return (id, crim_data)