-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMysqlOperator.py
53 lines (43 loc) · 1.49 KB
/
MysqlOperator.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# encoding=utf-8
'''
Created on 2016年5月10日
@author: dWX347607
'''
import MySQLdb
import logging
class MysqlOperator(object):
'''
mysql数据库操作
'''
def __init__(self, _host, _port, _user, _passwd, _db):
self.conn = None
self.cur = None
self.connect(_host, _port, _user, _passwd, _db)
def connect(self, _host, _port, _user, _passwd, _db):
try:
self.conn = MySQLdb.connect(host=_host, user=_user, passwd=_passwd, db=_db, port=_port)
self.cur = self.conn.cursor()
except MySQLdb.Error, ex:
logging.error(ex)
def execute(self, sql):
try:
logging.info(sql)
self.cur.execute(sql)
return self.cur.fetchall()
except MySQLdb.Error, ex:
logging.error(ex)
def commit(self):
try:
self.conn.commit()
except MySQLdb.Error, ex:
logging.error(ex)
def update_t_sys_config(self, VALUE, CATEGORY, NAME, TENANT_ID):
sql = "UPDATE `t_sys_config` SET `VALUE`='%s' WHERE (`CATEGORY`='%s') AND (`NAME`='%s') AND (`TENANT_ID`='%s')" % (VALUE, CATEGORY, NAME, TENANT_ID)
logging.info(sql)
self.execute(sql)
def close(self):
try:
self.conn.close()
self.cur.close()
except MySQLdb.Error, ex:
logging.error(ex)