Изменил логику работы курсора

This commit is contained in:
Vladislav 2023-10-27 12:41:19 +03:00
parent 0b4942de7d
commit b3656ffc05

View File

@ -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,9 +134,10 @@ class Endpoint(Thread):
return values
def __read_last_db_regs(self):
self.cursor.execute(f"select {self.db_col_names} from `{self.db_table}`"
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 = self.cursor.fetchall()
row = cursor.fetchall()
if len(row) != 0:
return row[0]
else:
@ -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()