forked from Sud0u53r/Exam-Panel-Flask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql.py
23 lines (21 loc) · 759 Bytes
/
sql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import pymysql
import pymysql.cursors
class SQLConnection:
credentials = {'host': 'localhost', 'user': 'XXXXXXXXXX', 'passwd' : 'XXXXXXXXXX', 'db': 'XXXXXXXXXX'}
def __init__(self, Format = 'dict', **kwargs):
self.Format = Format
def runQuery(self, query, params = None):
if self.Format == 'list': self.conn = pymysql.Connect(**self.credentials)
elif self.Format == 'dict': self.conn = pymysql.Connect(**self.credentials, cursorclass = pymysql.cursors.DictCursor)
try:
self.cursor = self.conn.cursor()
if params: self.cursor.execute(query, params)
else: self.cursor.execute(query)
self.conn.commit()
A = [row for row in self.cursor.fetchall()]
except:
A = []
finally:
self.conn.close()
return A
conn = SQLConnection ()