Repository navigation
Expand file tree
/
Copy pathsql_base.py
More file actions
57 lines (48 loc) · 1.73 KB
/
Copy pathsql_base.py
File metadata and controls
57 lines (48 loc) · 1.73 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
import sqlite3
class DataBase:
@staticmethod
def create_base():
"""Create a database with three columns: ID, username, password."""
with sqlite3.connect("users.sqlite") as conn:
cur = conn.cursor()
cur.execute(
"CREATE TABLE IF NOT EXISTS Users ("
"user_ID INTEGER PRIMARY KEY AUTOINCREMENT, "
"user_name TEXT, "
"password TEXT)"
)
# conn.close()
@staticmethod
def check_user(user_name: str, password: str) -> bool:
"""Check if there is a user and password in the database."""
conn = sqlite3.connect("users.sqlite")
cur = conn.cursor()
cur.execute(
f"SELECT user_name, password FROM Users WHERE user_name = ?", (user_name,)
)
val = cur.fetchone()
tup = (user_name, password)
if val == tup:
return True
return False
@staticmethod
def add_user(user: str, password: str) -> None:
"""Add new user to the database."""
with sqlite3.connect("users.sqlite") as conn:
cur = conn.cursor()
cur.execute(
"INSERT INTO Users (user_ID, user_name, password) VALUES (NULL, ?, ?)",
(user, password),
)
conn.commit()
@staticmethod
def check_username(user_name: str) -> str:
"""Check if there is a user with given username in the database."""
with sqlite3.connect("users.sqlite") as conn:
cur = conn.cursor()
cur.execute(
f"SELECT user_name FROM Users WHERE user_name = ?", (user_name,)
)
val = cur.fetchone()
if val:
return val[0]