-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.py
30 lines (27 loc) · 924 Bytes
/
mysql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import pymysql
class Mysql():
def __init__(self, address, db, name, password):
self.ok = True
try:
self.con = pymysql.connect(host=address, user=name, password=password, db=db, cursorclass=pymysql.cursors.DictCursor)
with self.con.cursor() as cur:
cur.execute("select version()")
ver = cur.fetchone()
except pymysql.Error:
self.ok = False
def getEntry(self, command):
if not self.ok:
yield None
with self.con.cursor() as cur:
cur.execute(command)
for i in range(cur.rowcount):
yield cur.fetchone()
def writeEntry(self, command):
if not self.ok:
return
try:
with self.con.cursor() as cur:
cur.execute(command)
self.con.commit()
except:
self.con.rollback()