diff options
| author | Andrew <saintruler@gmail.com> | 2019-07-24 14:55:21 +0400 |
|---|---|---|
| committer | Andrew <saintruler@gmail.com> | 2019-07-24 14:55:21 +0400 |
| commit | e38a785445b12b696beefb4cfef815ecfe89711d (patch) | |
| tree | b7042d03711ca4eb06aaf3e37f0fcbe9e398d56f /day9/task5_vue/backend/database/wrappers.py | |
| parent | 0e1cb6bad83ffd7377698eaccf07d35d504de5fa (diff) | |
Обновлена обертка для mysql баз данных.
Исправлены ошибки в валидаторах.
Исправлена обработка загрузки csv файла с новыми данными.
Diffstat (limited to 'day9/task5_vue/backend/database/wrappers.py')
| -rw-r--r-- | day9/task5_vue/backend/database/wrappers.py | 68 |
1 files changed, 32 insertions, 36 deletions
diff --git a/day9/task5_vue/backend/database/wrappers.py b/day9/task5_vue/backend/database/wrappers.py index 5bb9761..d380b2e 100644 --- a/day9/task5_vue/backend/database/wrappers.py +++ b/day9/task5_vue/backend/database/wrappers.py @@ -46,7 +46,7 @@ class MySQLWrapper(Wrapper): self.connection = MySQLdb.connect( host=host, user=username, - passwd=password, + password=password, db=db_name ) @@ -54,19 +54,18 @@ class MySQLWrapper(Wrapper): self.connection.close() def clear_table(self, table_name): - cursor = self.connection.cursor() - cursor.execute(f"START TRANSACTION; DELETE FROM `{table_name}`; COMMIT;") - cursor.close() + with self.connection.cursor() as cursor: + cursor.execute(f"DELETE FROM `{table_name}`;") def get_column_names(self): - cursor = self.connection.cursor() - cursor.execute('DESCRIBE table_task1;') - table_structure = cursor.fetchall() + with self.connection.cursor() as cursor: + cursor.execute('DESCRIBE table_task1;') + table_structure = cursor.fetchall() + table_headers = [field[0] for field in table_structure] return table_headers def insert_one(self, table_name, data_row: dict): - cursor = self.connection.cursor() scheme = self.schemes[table_name] field_names = [] @@ -74,21 +73,23 @@ class MySQLWrapper(Wrapper): for field_name, value in data_row.items(): field_names.append(f'`{field_name}`') - if scheme.fields[field_name].data_type == str: + if scheme.fields[field_name].nullable and value is None: + values.append('NULL') + elif scheme.fields[field_name].data_type == str: values.append(f'"{value}"') else: - values.append(value) + values.append(str(value)) - request = "START TRANSACTION; INSERT INTO `{}` ({}) VALUES ({}); COMMIT;".format( + request = "INSERT INTO `{}` ({}) VALUES ({});".format( table_name, ",".join(field_names), ",".join(values) ) - cursor.execute(request) - cursor.close() + with self.connection.cursor() as cursor: + cursor.execute(request) - def update(self, table_name, expressions, conditions): - cursor = self.connection.cursor() + self.connection.commit() + def update(self, table_name, expressions, conditions): expressions_formatted = [] for field_name, value in expressions.items(): if value != 'NULL' or not value.isnumeric(): @@ -101,48 +102,43 @@ class MySQLWrapper(Wrapper): value = f'"{value}"' conditions_formatted.append(f'`{field_name}`={value}') - cursor.execute("START TRANSACTION; UPDATE `{}` SET {} WHERE {}; COMMIT;".format( - table_name, ','.join(expressions_formatted), ' AND '.join(conditions_formatted) - )) + with self.connection.cursor() as cursor: + cursor.execute("UPDATE `{}` SET {} WHERE {};".format( + table_name, ','.join(expressions_formatted), ' AND '.join(conditions_formatted) + )) - cursor.close() + self.connection.commit() def delete_from(self, table_name, conditions): - cursor = self.connection.cursor() - conditions_formatted = [] for field_name, value in conditions.items(): if value != 'NULL' or not value.isnumeric(): value = f'"{value}"' conditions_formatted.append(f'`{field_name}`={value}') - cursor.execute("START TRANSACTION; DELETE FROM `{}` WHERE {}; COMMIT;".format( - table_name, ' AND '.join(conditions_formatted) - )) + with self.connection.cursor() as cursor: + cursor.execute("DELETE FROM `{}` WHERE {};".format( + table_name, ' AND '.join(conditions_formatted) + )) - cursor.close() + self.connection.commit() def get_data(self, table_name): - cursor = self.connection.cursor() + with self.connection.cursor() as cursor: + cursor.execute(f'SELECT * FROM `{table_name}`;') + content = list(map(list, cursor.fetchall())) - cursor.execute(f'SELECT * FROM `{table_name}`;') - content = list(map(list, cursor.fetchall())) - - cursor.close() return content def get_rows(self, table_name, conditions): - cursor = self.connection.cursor() - conditions_formatted = [] for field_name, value in conditions.items(): if value != 'NULL' or not value.isnumeric(): value = f'"{value}"' conditions_formatted.append(f'`{field_name}`={value}') - cursor.execute(f'SELECT * FROM `{table_name}` WHERE {" AND ".join(conditions_formatted)};') - content = list(map(list, cursor.fetchall())) - - cursor.close() + with self.connection.cursor() as cursor: + cursor.execute(f'SELECT * FROM `{table_name}` WHERE {" AND ".join(conditions_formatted)};') + content = list(map(list, cursor.fetchall())) return content |