-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFDataBase.py
More file actions
117 lines (96 loc) · 3.84 KB
/
FDataBase.py
File metadata and controls
117 lines (96 loc) · 3.84 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
import math
import sqlite3
import time
class FDataBase:
def __init__(self, db):
self.__db = db
self.__cur = db.cursor()
def getMenu(self):
sql = '''SELECT * FROM mainmenu'''
try:
self.__cur.execute(sql)
res = self.__cur.fetchall()
if res:
return res
except Exception as e:
print(f'Ошибка чтения БД, {e}')
return []
def addPost(self, title, text, url):
try:
self.__cur.execute(f"SELECT COUNT() as 'count' FROM posts WHERE url LIKE '{url}'")
res = self.__cur.fetchone()
if res['count'] > 0:
print('Статья с таким url уже существует')
return False
tm = math.floor(time.time())
self.__cur.execute("INSERT INTO posts VALUES(NULL, ?, ?, ?, ?)", (title, text, url, tm))
self.__db.commit()
except sqlite3.Error as e:
print(f'Ошибка добавления статьи в БД {e}')
return False
return True
def getPost(self, alias):
try:
self.__cur.execute(f"SELECT title, text FROM posts WHERE url LIKE '{alias}' LIMIT 1")
res = self.__cur.fetchone()
if res:
return res
except sqlite3.Error as e:
print(f'Ошибка получения статьи из БД {e}')
return False, False
def getPostsAnnounce(self):
try:
self.__cur.execute(f'SELECT id, title, text, url FROM posts ORDER BY time DESC')
res = self.__cur.fetchall()
if res:
return res
except sqlite3.Error as e:
print(f'Ошибка получения статьи из БД {e}')
return []
def addUser(self, name, email, hpsw):
try:
self.__cur.execute(f'SELECT COUNT() as "count" FROM users WHERE email LIKE "{email}"')
res = self.__cur.fetchone()
if res['count'] > 0:
print('Пользователь с таким email уже существует')
return False
tm = math.floor(time.time())
self.__cur.execute("INSERT INTO users VALUES (NULL, ?, ?, ?, NULL, ?)", (name, email, hpsw, tm))
self.__db.commit()
except sqlite3.Error as e:
print(f'Ошибка добавление пользователя в БД {e}')
return False
return True
def getUser(self, user_id):
try:
self.__cur.execute(f"SELECT * FROM users WHERE id = {user_id} LIMIT 1")
res = self.__cur.fetchone()
if not res:
print('Пользователь не найден')
return False
return res
except sqlite3.Error as e:
print(f"Ошибка получения данных из БД {e}")
return False
def getUserByEmail(self, email):
try:
self.__cur.execute(f"SELECT * FROM users WHERE email = '{email}' LIMIT 1")
res = self.__cur.fetchone()
if not res:
print('Пользователь не найден')
return False
return res
except sqlite3.Error as e:
print(f"Ошибка получения данных из БД {e}")
return False
def updateUserAvatar(self, avatar, user_id):
if not avatar:
return False
try:
binary = sqlite3.Binary(avatar)
self.__cur.execute(f"UPDATE users SET avatar = ? WHERE id = ?", (binary, user_id))
self.__db.commit()
except sqlite3.Error as e:
print(f'Ошибка обновления аватара в БД {e}')
return False
return True