Wraps method 'pymysql…execute()', adds 'autocommit'

This commit is contained in:
Fred Z 2018-08-03 18:23:09 +02:00
parent 5d5646ca9d
commit b18a7062ca
2 changed files with 21 additions and 0 deletions

View File

@ -16,6 +16,7 @@ DB_CONFIG = {
'password': 'loff',
'db': 'loff',
'charset': 'utf8',
'autocommit': True,
'file': 'create-db-loff.sql'
}

20
db.py
View File

@ -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
}