Repository navigation
Expand file tree
/
Copy pathdatabase.py
More file actions
190 lines (169 loc) · 7.38 KB
/
Copy pathdatabase.py
File metadata and controls
190 lines (169 loc) · 7.38 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
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
import sqlite3
import hashlib
import random
import time
class DataBase:
file_path = None
def __init__(self, path: str) -> None:
self.file_path = path
with sqlite3.connect(path) as con:
cur = con.cursor()
# 初始化用户表
cur.execute("DROP TABLE IF EXISTS users;") # 删除旧表
# 创建新用户表
cur.execute("""CREATE TABLE IF NOT EXISTS users(
id text PRIMARY KEY,
passwd text NOT NULL,
car text NOT NULL,
name text NOT NULL,
phone text NOT NULL,
state boolean,
wallet float,
times int
);""")
# 用户默认密码
dpasswd = 'e10adc3949ba59abbe56e057f20f883e'
# 添加用户
cur.execute("INSERT INTO USERS (id, passwd, car, name, phone, state, wallet, times) \
VALUES(?, ?, ?, ?, ?, ?, ?, ?);", ("36606b60", dpasswd, "A78A54", "沈华强", "13988776655", False, random.randint(1000, 9999)/100, -1))
cur.execute("INSERT INTO USERS (id, passwd, car, name, phone, state, wallet, times) \
VALUES(?, ?, ?, ?, ?, ?, ?, ?);", ("e3f6e22e", dpasswd, "D123A5", "彩须坤", "13088226655", False, random.randint(1000, 9999)/100, -1))
# 初始化车位表
cur.execute("DROP TABLE IF EXISTS parks;") # 删除旧车位表
cur.execute("""CREATE TABLE IF NOT EXISTS parks(
id text PRIMARY KEY,
state boolean NOT NULL,
zone text NOT NULL
);""")
# 添加车位
# A区
for i in range(4):
cur.execute("INSERT INTO PARKS (id, state, zone) VALUES(?, ?, ?);", (f"A{i}", False, "A"))
# B区
for i in range(8):
cur.execute("INSERT INTO PARKS (id, state, zone) VALUES(?, ?, ?);", (f"B{i}", random.choice([True, False]), "B"))
# C区
for i in range(8):
cur.execute("INSERT INTO PARKS (id, state, zone) VALUES(?, ?, ?);", (f"C{i}", random.choice([True, False]), "C"))
# D区
for i in range(8):
cur.execute("INSERT INTO PARKS (id, state, zone) VALUES(?, ?, ?);", (f"D{i}", random.choice([True, False]), "D"))
# E区
for i in range(8):
cur.execute("INSERT INTO PARKS (id, state, zone) VALUES(?, ?, ?);", (f"E{i}", random.choice([True, False]), "E"))
def add_user(user: dict) -> bool:
pass
# 获取所有用户数据
def get_all_users(self) -> tuple[dict]:
pass
# 根据ID获取一个用户的基本数据
def get_user_byID(self, id: str) -> dict:
user = dict()
with sqlite3.connect(self.file_path) as con:
cur = con.cursor()
res = cur.execute("SELECT id, car, name, phone FROM users WHERE id = ?;", (id,))
for row in res:
user["id"] = row[0]
user["car"] = row[1]
user["name"] = row[2]
user["phone"] = row[3]
return user
# 根据id获取一个用户的车辆信息
def get_car_byID(self, userid: str) -> dict:
car_info = {
'car': None,
'state': None,
'times': None
}
with sqlite3.connect(self.file_path) as con:
cur = con.cursor()
res = cur.execute("SELECT car, state, times FROM users WHERE id = ?;", (userid,))
for row in res:
car_info["car"] = row[0]
car_info["state"] = row[1]
car_info["times"] = row[2]
return car_info
# 车辆入库
def car_in(self, userid: str):
with sqlite3.connect(self.file_path) as con:
cur = con.cursor()
# 设置车辆状态
cur.execute("UPDATE users SET state = ? WHERE id = ?;", (True, userid))
cur.execute("UPDATE users SET times = ? WHERE id = ?;", (int(time.time()), userid))
# 车辆出库
def car_out(self, userid: str):
with sqlite3.connect(self.file_path) as con:
cur = con.cursor()
# 设置车辆状态
cur.execute("UPDATE users SET state = ? WHERE id = ?;", (False, userid))
cur.execute("UPDATE users SET times = ? WHERE id = ?;", (-1, userid))
# 修改用户的基本信息
def chuser_info(self, id: str, username: str, car: str, phone: str) -> int:
with sqlite3.connect(self.file_path) as con:
cur = con.cursor()
cur.execute("UPDATE USERS SET name = ? WHERE id = ?;", (username, id))
cur.execute("UPDATE USERS SET car = ? WHERE id = ?;", (car, id))
cur.execute("UPDATE USERS SET phone = ? WHERE id = ?;", (phone, id))
# 验证用户登录信息
def check_user(self, id: str, passwd: str) -> int:
# 从数据库中读取用户信息
result = None
with sqlite3.connect(self.file_path) as con:
cur = con.cursor()
cur.execute("SELECT passwd FROM users WHERE id = ?;", (id,))
result = cur.fetchone()
# 判断用户是否在数据库中
if result == None:
return -1
# 判断密码是否正确
elif result[0] == hashlib.md5(passwd.encode("utf-8")).hexdigest():
return 1
else:
return 0
# 获取用户钱包状态
def get_user_wallet(self, userid: str) -> dict:
wallet = dict()
wallet['id'] = userid
with sqlite3.connect(self.file_path) as con:
cur = con.cursor()
res = cur.execute("SELECT wallet, times FROM users WHERE id = ?;", (userid,))
for row in res:
wallet['wallet'] = row[0]
wallet['times'] = row[1]
return wallet
# 用户充值
def topup(self, userid, amount):
try:
with sqlite3.connect(self.file_path) as con:
cur = con.cursor()
cur.execute("UPDATE USERS SET wallet = wallet + ? WHERE id = ?;", (amount, userid))
return 1
except Exception as e:
print(e)
return 0
# 用户扣费
def spend(self, userid, amount: float):
try:
with sqlite3.connect(self.file_path) as con:
cur = con.cursor()
cur.execute("UPDATE USERS SET wallet = wallet - ? WHERE id = ?;", (amount, userid))
return 1
except Exception as e:
print(e)
return 0
def get_parks(self, zone: str) -> list[int]:
parks = list()
with sqlite3.connect(self.file_path) as con:
cur = con.cursor()
res = cur.execute("SELECT state FROM parks WHERE zone = ?;", (zone,))
for row in res:
parks += (row[0],)
return parks
# 更新车位状态
def update_park(self, id: str, state: bool) -> int:
with sqlite3.connect(self.file_path) as con:
cur = con.cursor()
cur.execute("UPDATE PARKS SET state = ? WHERE id = ?;", (state, id))
return 1
# 实例化数据库对象
db = DataBase("data.db")