diff --git a/endpoint.py b/endpoint.py index a60f8f0..ccceab2 100644 --- a/endpoint.py +++ b/endpoint.py @@ -110,7 +110,6 @@ class Endpoint(Thread): # база данных self.connection = None - self.cursor = None self.db_table = config['database']['db-table'] self.db_col_names = '' for rn in self.ranges: @@ -135,13 +134,14 @@ class Endpoint(Thread): return values def __read_last_db_regs(self): - self.cursor.execute(f"select {self.db_col_names} from `{self.db_table}`" - f"order by `{self.config['database']['db-datetime-col']}` desc limit 1;") - row = self.cursor.fetchall() - if len(row) != 0: - return row[0] - else: - return None + with self.connection.cursor() as cursor: + cursor.execute(f"select {self.db_col_names} from `{self.db_table}`" + f"order by `{self.config['database']['db-datetime-col']}` desc limit 1;") + row = cursor.fetchall() + if len(row) != 0: + return row[0] + else: + return None def __insert_db_row(self, values): vals_str = "" @@ -153,7 +153,8 @@ class Endpoint(Thread): query = f"insert into `{self.db_table}` ({self.db_col_names}) values ({vals_str});" journal.log(f"Executing query: {query}") - self.cursor.execute(query) + with self.connection.cursor() as cursor: + cursor.execute(query) def __endless_loop(self): # if self.log_type == 'on-change': @@ -217,8 +218,7 @@ class Endpoint(Thread): database=self.config["database"]["db-name"] ) as _connection: self.connection = _connection - with self.connection.cursor() as cursor: - self.cursor = cursor - # запуск бесконечного цикла - self.__endless_loop() + # запуск бесконечного цикла + self.__endless_loop() +