-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathdb_service.py
51 lines (44 loc) · 1.7 KB
/
db_service.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
import sqlite3
class SQLiteDBHandler:
"""Работа с БД sqlite"""
_instance = None
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(SQLiteDBHandler, cls).__new__(cls)
return cls._instance
def __init__(self, db_name="database.db"):
if not hasattr(self, "_initialized"):
self.db_name = db_name
self._create_table()
self._initialized = True
def _create_table(self):
"""Создает таблицу viewed, если она не существует."""
with sqlite3.connect(self.db_name) as conn:
cursor = conn.cursor()
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS viewed (
id INTEGER,
price INTEGER
)
"""
)
conn.commit()
def add_record(self, record_id, price):
"""Добавляет новую запись в таблицу viewed."""
with sqlite3.connect(self.db_name) as conn:
cursor = conn.cursor()
cursor.execute(
"INSERT INTO viewed (id, price) VALUES (?, ?)",
(record_id, price),
)
conn.commit()
def record_exists(self, record_id, price):
"""Проверяет, существует ли запись с заданными id и price."""
with sqlite3.connect(self.db_name) as conn:
cursor = conn.cursor()
cursor.execute(
"SELECT 1 FROM viewed WHERE id = ? AND price = ?",
(record_id, price),
)
return cursor.fetchone() is not None