-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
195 lines (127 loc) · 4.67 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import sqlite3
database_name = "database.db"
#def collation_no_accent():
#Initialisation Bdd
def ini_database():
conn = sqlite3.connect(database_name)
conn.execute("PRAGMA foreign_keys = ON")
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE If NOT EXISTS users(
user_uid TEXT UNIQUE NOT NULL PRIMARY KEY,
nom TEXT NOT NULL,
prenom TEXT NOT NULL,
num_etu INTEGER UNIQUE NOT NULL,
max_locker INTEGER DEFAULT 1
)
""")
cursor.execute("""
CREATE TABLE If NOT EXISTS casiers (
casier_id INTEGER NOT NULL PRIMARY KEY,
i2c_adr TEXT NOT NULL,
in_pin INT NOT NULL,
out_pin INT NOT NULL,
owner_uid TEXT,
FOREIGN KEY (owner_uid)
REFERENCES users (user_uid)
ON DELETE SET NULL
ON UPDATE CASCADE
)
""")
conn.commit()
#ajouter un casier à la base de donnée
def add_casier(casier_id, i2c_adr, in_pin, out_pin):
conn = sqlite3.connect(database_name)
conn.execute("PRAGMA foreign_keys = ON")
cursor = conn.cursor()
cursor.execute("""INSERT INTO casiers(casier_id, i2c_adr, in_pin, out_pin) VALUES(?,?,?,?)""", (casier_id, i2c_adr, in_pin, out_pin,))
conn.commit()
#ajouter un utilisateur à la base de donnée
def add_user(user_uid, num_etu, nom, prenom):
conn = sqlite3.connect(database_name)
conn.execute("PRAGMA foreign_keys = ON")
cursor = conn.cursor()
cursor.execute("""INSERT INTO users(user_uid, num_etu, nom, prenom) VALUES(?,?,?,?)""", (user_uid, num_etu, nom, prenom,))
conn.commit()
def add_user_to_casier(casier_id, owner_uid):
conn = sqlite3.connect(database_name)
conn.execute("PRAGMA foreign_keys = ON")
cursor = conn.cursor()
cursor.execute("""UPDATE casiers SET owner_uid = ? WHERE casier_id = ?""", (owner_uid, casier_id,))
conn.commit()
def select_all_casier():
conn = sqlite3.connect(database_name)
cursor = conn.cursor()
cursor.execute("""SELECT * FROM casiers""")
return cursor.fetchall()
def select_casier_by_id(id):
conn = sqlite3.connect(database_name)
cursor = conn.cursor()
cursor.execute("""SELECT * FROM casiers WHERE casier_id = ?""", (id,))
return cursor.fetchall()[0]
def select_user_by_uid(uid):
conn = sqlite3.connect(database_name)
cursor = conn.cursor()
cursor.execute("""SELECT nom, prenom, num_etu FROM users WHERE user_uid=?""", (uid,))
return cursor.fetchall()[0]
def select_user():
conn = sqlite3.connect(database_name)
cursor = conn.cursor()
cursor.execute("""
SELECT nom, prenom, user_uid
FROM users
ORDER BY nom COLLATE NOCASE ASC, prenom COLLATE NOCASE ASC
""")
return cursor.fetchall()
def partial_select(str):
conn = sqlite3.connect(database_name)
cursor = conn.cursor()
cursor.execute("""
SELECT *
FROM users
WHERE UPPER(nom || ' ' || prenom) GLOB UPPER(?)
OR UPPER(prenom || ' ' || nom) GLOB UPPER(?)
""", (str+"*", str+"*",))
return cursor.fetchall()
def partial_select_num(num_etu):
conn = sqlite3.connect(database_name)
cursor = conn.cursor()
cursor.execute("""
SELECT *
FROM users
WHERE CAST(num_etu AS TEXT) LIKE ?
""", (str(num_etu)+"%",))
return cursor.fetchall()
def set_max_locker(id, max):
conn = sqlite3.connect(database_name)
conn.execute("PRAGMA foreign_keys = ON")
cursor = conn.cursor()
cursor.execute("""UPDATE users SET max_locker = ? WHERE user_uid = ?""", (max, id,))
conn.commit()
def get_max_locker(uid):
conn = sqlite3.connect(database_name)
conn.execute("PRAGMA foreign_keys = ON")
cursor = conn.cursor()
cursor.execute("""SELECT max_locker FROM users WHERE user_uid=?""", (uid,))
return cursor.fetchall()[0][0]
#test des fonctions
ini_database()
"""
for i in range (10, 50):
add_user(i, "valentin", str(i))
"""
"""add_user("504565", 21902626, "gousset", "mda")
add_user("55", 21902626,"toto", "tata")
add_user("4744787", 21902626, "beta", "luc")
add_user("bgfdyuzgbqui", 21902626, "L'hermite", "Valentin")
add_user("154564664", 21902626, "l'hermite", "valentin")
add_user("1234", 21902626, "Bob", "Eponge")"""
#print(select_user()
#add_user("47456456", "bébé", "yoda")
#print(get_max_locker("47456456"))
#set_max_locker("47456456", 2)
#print(get_max_locker("47456456"))
#print(partial_select("v"))
#add_casier(1, "0x20", 1, 2)
print(select_all_casier())
print(partial_select_num(3))