-
Notifications
You must be signed in to change notification settings - Fork 13
/
quick_tools.py
88 lines (53 loc) · 2.01 KB
/
quick_tools.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
import sqlite3
def get_rooms(db_path):
connect = sqlite3.connect(db_path)
cursor = connect.cursor()
sql = 'SELECT room_name FROM Rooms;'
rooms = cursor.execute(sql ).fetchall()
rooms = [room[0] for room in rooms]
return rooms
def add_room(db_path, room_name, room_type):
connect = sqlite3.connect(db_path)
cursor = connect.cursor()
sql = 'INSERT INTO Rooms (room_name,room_type) VALUES (?,?)'
cursor.execute(sql,(room_name, room_type))
connect.commit()
def delete_room(db_path, room_name):
connect = sqlite3.connect(db_path)
cursor = connect.cursor()
sql = 'DELETE FROM Rooms WHERE room_name=?'
cursor.execute(sql,(room_name,))
connect.commit()
def get_users(db_path):
connect = sqlite3.connect(db_path)
cursor = connect.cursor()
sql = 'SELECT user_name FROM Users;'
users = cursor.execute(sql ).fetchall()
users = [user[0] for user in users]
return users
def add_user(db_path, user_name, user_role, user_rights, user_password):
connect = sqlite3.connect(db_path)
cursor = connect.cursor()
sql = 'INSERT INTO Users (user_name, user_role, user_rights, user_password) VALUES (?,?,?,?)'
cursor.execute(sql,(user_name, user_role, user_rights, user_password))
connect.commit()
def delete_user(db_path, user_name):
connect = sqlite3.connect(db_path)
cursor = connect.cursor()
sql = 'DELETE FROM Users WHERE user_name=?'
cursor.execute(sql,(user_name,))
connect.commit()
def create_db(db_path):
connect = sqlite3.connect(db_path)
cursor = connect.cursor()
cursor.execute('CREATE TABLE Rooms ([id_room] INTEGER PRIMARY KEY,[room_name] text UNIQUE, [room_type] text)')
cursor.execute('CREATE TABLE Users ([id_user] INTEGER PRIMARY KEY,[user_name] text UNIQUE, [user_role] integer, [user_rights] integer, [user_password] text)')
connect.commit()
# Db creation :
# db_path = 'quick_chat.db'
# create_db(db_path)
# add_user('quick_chat.db','yann.c',0,0,'password')
# add_room('quick_chat.db','room0','public')
# print(get_users(db_path))
# print(get_rooms(db_path))
# delete_user(db_path,'yann.c')