Created
July 13, 2017 03:32
-
-
Save AnyISalIn/8c6fb8f0bf3dac7dd95c46b52a70b8ee to your computer and use it in GitHub Desktop.
SQLExecutor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pymysql | |
from pymysql.cursors import DictCursor | |
class SQLExecutor(object): | |
def __init__(self, *args, **kwargs): | |
self._conn = pymysql.Connect(*args, **kwargs) | |
def _execute(self, sql): | |
with self._conn as cur: | |
cur.execute(sql) | |
cur.close() | |
return cur.fetchall() | |
def execute(self, sql): | |
return self._execute(sql) | |
""" | |
Usage: | |
>>> sql = SQLExecutor(host='127.0.0.1', user='root', password='passwd', cursorclass=DictCursor) | |
>>> sql.execute('show databases') | |
[{'Database': 'information_schema'}, | |
{'Database': 'mysql'}, | |
{'Database': 'performance_schema'}, | |
{'Database': 'sys'}] | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment