-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDAO_user.py
More file actions
63 lines (53 loc) · 2.15 KB
/
DAO_user.py
File metadata and controls
63 lines (53 loc) · 2.15 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
# Data access object pattern
# To separate business layer from persistence layer
class DAO_user:
def __init__(self, db_connection):
self.db_connection = db_connection
def read_users(self):
get_users = "SELECT * FROM users"
query = self.db_connection.execute(get_users)
users = []
for c in query:
users.append(c)
return users
def update_user(self, id_user, column, upd):
stringCol = ""
if column == 0:
stringCol = "name"
elif column == 1:
stringCol = "lastname"
elif column == 2:
stringCol = "gender"
elif column == 3:
stringCol = "bd"
else:
stringCol = "civilStatus"
query = "update users SET %s = '%s' WHERE id = %d" % (stringCol, upd, id_user)
self.db_connection.execute(query)
self.db_connection.commit()
def delete_user(self, id_user):
query_delete_user = "DELETE FROM users WHERE id = %d ;" % (id_user)
self.db_connection.execute(query_delete_user)
self.db_connection.commit()
def create_user(self, user):
query_not_existing_table = "CREATE TABLE IF NOT EXISTS users("\
"id int PRIMARY KEY NOT NULL,"\
"name TEXT NOT NULL, lastname TEXT NOT NULL,"\
"gender TEXT NOT NULL, bd TEXT NOT NULL,"\
"civilStatus TEXT NOT NULL);"
self.db_connection.execute(query_not_existing_table)
self.db_connection.commit()
cursor = self.db_connection.execute("SELECT id FROM users ORDER BY id DESC LIMIT 1;")
last_id = None
for row in cursor:
last_id = int(row[0])
break
if last_id != None:
next_id = last_id + 1
else:
next_id = 0
self.db_connection.execute("INSERT INTO users (id, name, lastname, gender, \
bd, civilStatus) VALUES (?,?,?,?,?,?);" , (next_id,
user.name, user.lastname, user.gender, user.bd,
user.civilStatus))
self.db_connection.commit()