-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
66 lines (55 loc) · 1.85 KB
/
db.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
import sqlite3
from mocks import User
class DataBase:
def __init__(self, db_name="users.db"):
self.name = db_name
conn = self.__get_connection()
try:
cursor = conn.cursor()
cursor.execute("""CREATE TABLE users (
tg_id integer,
first_name text,
username text,
last_name text,
language_code text,
supports_inline_queries null,
registration_date integer,
remove_bg_api_key text
)
""")
conn.commit()
conn.close()
except Exception as e:
conn.close()
print("[!]", e.args[0])
def __get_connection(self):
return sqlite3.connect(self.name)
def user_not_exist(self, user: User):
if self.find(user) is None:
return True
else:
return False
def add(self, user: User):
conn = self.__get_connection()
cursor = conn.cursor()
user_data = tuple(user)
cursor.execute(f"INSERT INTO users VALUES (?,?,?,?,?,?,?,?)", user_data)
conn.commit()
conn.close()
def find(self, user: User):
conn = self.__get_connection()
cursor = conn.cursor()
user = cursor.execute(f"SELECT * FROM users WHERE tg_id='{user.tg_id}'")
user_tuple = user.fetchone()
if user_tuple is None:
return None
user = User(tuple_=user_tuple)
return user
def update(self, user: User):
conn = self.__get_connection()
cursor = conn.cursor()
cursor.execute(f"""UPDATE users SET remove_bg_api_key='{user.api_key}' WHERE tg_id='{user.tg_id}'""")
conn.commit()
conn.close()
if __name__ == "__main__":
db = DataBase()