From b18a7062caf45a656691b42cf6b596b957898fd6 Mon Sep 17 00:00:00 2001 From: Fred Z Date: Fri, 3 Aug 2018 18:23:09 +0200 Subject: [PATCH] =?UTF-8?q?Wraps=20method=20'pymysql=E2=80=A6execute()',?= =?UTF-8?q?=20adds=20'autocommit'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.py | 1 + db.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/config.py b/config.py index ac67028..36903a0 100644 --- a/config.py +++ b/config.py @@ -16,6 +16,7 @@ DB_CONFIG = { 'password': 'loff', 'db': 'loff', 'charset': 'utf8', + 'autocommit': True, 'file': 'create-db-loff.sql' } diff --git a/db.py b/db.py index a5a838e..8704f14 100644 --- a/db.py +++ b/db.py @@ -128,6 +128,25 @@ class Db(): else: return summary + def execute(self, sql_request): + """ + Executes a request on DB + + :Tests: + >>> Db.start_connexion(Db, with_db=True) + >>> Db.execute(Db, 'SHOW TABLES') + 2 + >>> Db.result == [{'Tables_in_loff': 'category'}, \ +{'Tables_in_loff': 'product'}] + True + """ + # Connect to the database + self.start_connexion(self) + + response = self.cursor.execute(sql_request) + self.result = self.cursor.fetchall() + return response + def get_sql_from_file(self, filename=DB_CONFIG['file']): """ Get the SQL instruction from a file @@ -177,6 +196,7 @@ to database 'foobar'")ยป 'user': DB_CONFIG['user'], 'password': DB_CONFIG['password'], 'charset': DB_CONFIG['charset'], + 'autocommit': DB_CONFIG['autocommit'], 'cursorclass': pymysql.cursors.DictCursor }