From f48a05611c290e2dddc91c9860ee99eeca580a81 Mon Sep 17 00:00:00 2001 From: Eduard Date: Fri, 23 May 2025 15:45:25 +0300 Subject: [PATCH] self.cursor --- db/db.py | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/db/db.py b/db/db.py index 836ad41..1cf57cf 100644 --- a/db/db.py +++ b/db/db.py @@ -10,13 +10,13 @@ logger = logging.getLogger(__name__) class Database: def __init__(self, db_name: str, sql_path: str): self.con = sq.connect(db_name) - cursor = self.con.cursor() + self.cursor = self.con.cursor() try: with open(sql_path, 'r', encoding='utf-8') as f: sql_script = f.read() - cursor.executescript(sql_script) + self.cursor.executescript(sql_script) self.con.commit() logger.info("База данных успешно инициализирована!") except Exception as e: @@ -25,16 +25,15 @@ class Database: # Сделать изменение всех полей при IntegrityError def insert_activities(self, activities: list[Activity]): - cur = self.con.cursor() is_updated = True for act in activities: if is_updated: - tags = cur.execute("SELECT * FROM tags WHERE id = ?", (act.tag_id, )).fetchall() - vetro_projects = cur.execute("SELECT * FROM vetro_projects WHERE id = ?", (act.vetro_project_id, )).fetchall() + tags = self.cursor.execute("SELECT * FROM tags WHERE id = ?", (act.tag_id, )).fetchall() + vetro_projects = self.cursor.execute("SELECT * FROM vetro_projects WHERE id = ?", (act.vetro_project_id, )).fetchall() if not tags or not vetro_projects: is_updated = False try: - cur.execute( + self.cursor.execute( "INSERT INTO activities VALUES(?,?,?,?,?,?,?,?,?)", ( act.id, @@ -49,7 +48,7 @@ class Database: ) ) except sq.IntegrityError: - cur.execute( + self.cursor.execute( """UPDATE activities SET description = ?, time_spent = ?, date_start = ?, date_end = ?, is_tracked = ?, vetro_project_id = ?, tag_id = ?, project_id = ? WHERE id = ?""", @@ -71,10 +70,9 @@ class Database: logging.info("База данных устарела. Рекомендуем обновить с помощью флага -i [--init-db]") def insert_time_entries(self, activities: list[Activity]): - cur = self.con.cursor() for act in activities: try: - cur.execute( + self.cursor.execute( """INSERT INTO time_entry (date, description, activity_id, time_spent, task_id, activity_type_id) VALUES(?,?,?,?,?,?)""", @@ -93,12 +91,11 @@ class Database: def insert_tags(self, tags: list[Tag]): - cur = self.con.cursor() for tag in tags: try: - cur.execute("INSERT INTO tags VALUES(?, ?)",(tag.id,tag.title)) + self.cursor.execute("INSERT INTO tags VALUES(?, ?)",(tag.id,tag.title)) except sq.IntegrityError: - cur.execute( + self.cursor.execute( """UPDATE tags SET title = ? WHERE id = ?""", @@ -109,12 +106,11 @@ class Database: def insert_vetro_projects(self, vetro_projects: list[VetroProject]): - cur = self.con.cursor() for vetro_project in vetro_projects: try: - cur.execute("INSERT INTO vetro_projects VALUES(?, ?)", (vetro_project.id, vetro_project.title)) + self.cursor.execute("INSERT INTO vetro_projects VALUES(?, ?)", (vetro_project.id, vetro_project.title)) except sq.IntegrityError: - cur.execute(""" + self.cursor.execute(""" UPDATE vetro_projects SET title = ? WHERE id = ?""", @@ -124,12 +120,11 @@ class Database: self.con.commit() def insert_activity_types(self, activity_types: list[ActivityType]): - cur = self.con.cursor() for activity_type in activity_types: try: - cur.execute("INSERT INTO activity_types VALUES(?, ?)", (activity_type.id, activity_type.title)) + self.cursor.execute("INSERT INTO activity_types VALUES(?, ?)", (activity_type.id, activity_type.title)) except sq.IntegrityError: - cur.execute(""" + self.cursor.execute(""" UPDATE activity_types SET title = ? WHERE id = ?""",