-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.py
147 lines (132 loc) · 5.64 KB
/
db.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
142
143
144
145
146
147
import pymysql
from users import User
dbServerName = "localhost"
dbUser = "root"
dbPassword = "ilikecode"
dbName = "users"
charSet = "utf8mb4"
cursorType = pymysql.cursors.DictCursor
def login(username, password):
connectionObject = pymysql.connect(host = dbServerName,
user = dbUser,
password = dbPassword,
db = dbName,
charset = charSet,
cursorclass = cursorType)
try:
with connectionObject.cursor() as cursor:
query = "SELECT * FROM users WHERE username=\""+username+"\" AND password = \""+password+"\""
# countquery = "SELECT COUNT(*) FROM USERS WHERE username = "+username+" " \
# "AND password = "+password+""
cursor.execute(query)
# cursor.execute(countquery)
rows = cursor.fetchall()
count = cursor.rowcount
if (count == 0):
return None
elif (count > 0):
rows[0]['logged_in'] = True
print(rows[0])
return rows[0]
return
# zero rows return none
# 1 or more return object with first row made into user class object
finally:
connectionObject.close()
def createaccount(info):
connectionObject = pymysql.connect(host = dbServerName,
user = dbUser,
password = dbPassword,
db = dbName,
charset = charSet,
cursorclass = cursorType)
try:
with connectionObject.cursor() as cursor:
query = "INSERT INTO users (username, password, last, first, company, email, type) VALUES (\""+info['username']+"\",\""+info['password']+"\",\""+info['last']+"\",\""+info['first']+"\",\""+info['company']+"\",\""+info['email']+"\","+info['type']+")"
print(query)
x = cursor.execute(query)
return x
finally:
connectionObject.commit()
connectionObject.close()
def readchain(info):
connectionObject = pymysql.connect(host = dbServerName,
user = dbUser,
password = dbPassword,
db = dbName,
charset = charSet,
cursorclass = cursorType)
try:
with connectionObject.cursor() as cursor:
query = "SELECT * FROM patients WHERE first=\""+info['firstx']+"\" AND last=\""+info['lastx']+"\""
cursor.execute(query)
rows = cursor.fetchall()
if(cursor.rowcount==0):
return False;
return rows[0];
finally:
connectionObject.close()
def writechain(info):
connectionObject = pymysql.connect(host = dbServerName,
user = dbUser,
password = dbPassword,
db = dbName,
charset = charSet,
cursorclass = cursorType)
try:
with connectionObject.cursor() as cursor:
query = "INSERT INTO patients (first, last, chain) VALUES (\""+info['first']+"\",\""+info['last']+"\",\""+info['chain']+"\")";
print(query)
x = cursor.execute(query)
return x
finally:
connectionObject.commit()
connectionObject.close()
# TODO refactor patient_id to be prescription_id
def readprescription(info):
connectionObject = pymysql.connect(host = dbServerName,
user = dbUser,
password = dbPassword,
db = dbName,
charset = charSet,
cursorclass = cursorType)
try:
with connectionObject.cursor() as cursor:
query = "SELECT * FROM meds WHERE patient_id=\""+info['patient_id']+"\" AND med=\""+info['prescription_id']+"\""
cursor.execute(query)
rows = cursor.fetchall()
if(cursor.rowcount==0):
return False;
return rows[0];
finally:
connectionObject.close()
def gethighest():
connectionObject = pymysql.connect(host = dbServerName,
user = dbUser,
password = dbPassword,
db = dbName,
charset = charSet,
cursorclass = cursorType)
try:
with connectionObject.cursor() as cursor:
query = "SELECT MAX(id) FROM meds";
x = cursor.execute(query)
return cursor.fetchall()[0]['MAX(id)']
finally:
connectionObject.close()
def writeprescription(info):
connectionObject = pymysql.connect(host = dbServerName,
user = dbUser,
password = dbPassword,
db = dbName,
charset = charSet,
cursorclass = cursorType)
try:
with connectionObject.cursor() as cursor:
query = "INSERT INTO meds (patient_id, hash, med) VALUES ("+str(info['patient_id'])+",\""+info['hash']+"\", \""+str(info['prescription_id'])+"\")";
print(query)
x = cursor.execute(query)
return x
finally:
connectionObject.commit()
connectionObject.close()