-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataBase.py
More file actions
101 lines (85 loc) · 3.66 KB
/
DataBase.py
File metadata and controls
101 lines (85 loc) · 3.66 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
import math
import time
import sqlite3
import re
from flask import url_for
class DataBase:
def __init__(self, db):
self.__db = db
self.__cur = db.cursor()
#Работа с пользователем
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, ?, ?, ?, ?)', (name, email, hpsw, tm))
self.__db.commit()
except sqlite3.Error as e:
print('Ошибка добавления пользователя в БД'+str(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('Ошибка во входе пользователя getUser'+str(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('Ошибка во входе пользователя getUserByEmail'+str(e))
# Работа с заметками
def getNotes(self, user_id):
try:
self.__cur.execute(f'SELECT id, title, preview, date FROM notes WHERE user_id = {user_id}')
res = self.__cur.fetchall()
if not res:
print('Заметки не найдены')
return []
return res
except sqlite3.Error as e:
print('Ошибка получения заметок: ' + str(e))
return []
def addNote(self, user_id, title, preview):
try:
tm = math.floor(time.time())
self.__cur.execute('INSERT INTO notes (user_id, title, preview, date) VALUES (?, ?, ?, ?)',
(user_id, title, preview, tm))
self.__db.commit()
except sqlite3.Error as e:
print('Ошибка добавления заметки: ' + str(e))
return False
return True
def deleteNote(self, note_id):
try:
self.__cur.execute(f'DELETE FROM notes WHERE id = {note_id}')
self.__db.commit()
except sqlite3.Error as e:
print('Ошибка удаления заметки: ' + str(e))
return False
return True
def updateNote(self, note_id, title, preview):
try:
tm = math.floor(time.time())
self.__cur.execute(f'UPDATE notes SET title = ?, preview = ?, date = ? WHERE id = ?',
(title, preview, tm, note_id))
self.__db.commit()
except sqlite3.Error as e:
print('Ошибка обновления заметки: ' + str(e))
return False
return True