-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.py
106 lines (77 loc) · 3.32 KB
/
Database.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
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
import sqlite3
import io
class Database:
# Инициализация
def __init__(self, db):
self.__db = db
self.__cur = db.cursor()
# Создание БД
def create_db(self, nameDb, scriptDbName):
try:
conn = sqlite3.connect(nameDb, check_same_thread=False)
conn.row_factory = sqlite3.Row
with io.open(scriptDbName, encoding='utf-8') as file:
self.__db.cursor().executescript(file.read())
self.__db.commit()
except sqlite3.Error as e:
print("Ошибка создания БД " + str(e))
return False
return True
# Добавление нового предмета в БД
def add_item(self, name):
try:
self.__cur.execute("INSERT INTO items VALUES(NULL, ?, ?)", (name, 0))
self.__db.commit()
except sqlite3.Error as e:
print("Ошибка добавления элемента " + str(e))
return False
return True
def get_items(self):
try:
with sqlite3.connect("main.db", check_same_thread=False) as con:
cur = con.cursor()
cur.execute(f"SELECT * FROM items")
con.commit()
res = cur.fetchall()
if not res:
print("Данные не найдены в таблице")
return False
return res
except sqlite3.Error as e:
print("Ошибка получения элементов " + str(e))
return False
######################################################################
# Добавление нового предмета в БД
def add_item_schedule(self, relativeItemId, status, startH, startM, endH, endM):
try:
self.__cur.execute("INSERT INTO schedule VALUES(NULL, ?, ?, ?, ?, ?, ?)",
(relativeItemId, status, startH, startM, endH, endM))
self.__db.commit()
except sqlite3.Error as e:
print("Ошибка добавления элемента " + str(e))
return False
return True
def get_schedule(self):
try:
with sqlite3.connect("main.db", check_same_thread=False) as con:
cur = con.cursor()
cur.execute(f"SELECT * FROM schedule")
con.commit()
res = cur.fetchall()
if not res:
print("Данные не найдены в таблице")
return False
return res
except sqlite3.Error as e:
print("Ошибка получения элементов " + str(e))
return False
def insert_state_by_id(self, item_id, state):
try:
with sqlite3.connect("main.db", check_same_thread=False) as con:
cur = con.cursor()
cur.execute(f"UPDATE items SET isBuyed = {state} WHERE id = {item_id}")
con.commit()
except sqlite3.Error as e:
print("Ошибка получения данных из БД " + str(e))
return False
return True