mdserver-web/plugins/mysql/class/mysqlDb.py

100 lines
3.1 KiB
Python
Raw Normal View History

2019-01-05 09:06:15 -05:00
# coding: utf-8
import re
import os
import sys
2021-05-10 06:08:38 -04:00
# sys.path.append("/usr/local/lib/python3.9/site-packages")
sys.path.append(os.getcwd() + "/class/core")
import mw
# if mw.isAppleSystem():
# cmd = 'ls /usr/local/lib/ | grep python | cut -d \\ -f 1 | awk \'END {print}\''
# info = mw.execShell(cmd)
# p = "/usr/local/lib/" + info[0].strip() + "/site-packages"
# sys.path.append(p)
2019-01-05 09:06:15 -05:00
2021-02-03 13:26:10 -05:00
class mysqlDb:
2019-01-05 09:06:15 -05:00
__DB_PASS = None
__DB_USER = 'root'
__DB_PORT = 3306
__DB_HOST = 'localhost'
__DB_CONN = None
__DB_CUR = None
__DB_ERR = None
__DB_CNF = '/etc/my.cnf'
def __Conn(self):
'''连接MYSQL数据库'''
try:
2020-07-10 03:57:25 -04:00
import mw
2019-01-05 09:06:15 -05:00
socket = '/tmp/mysql.sock'
try:
import MySQLdb
2021-05-09 12:08:14 -04:00
except Exception as ex:
2021-05-10 06:08:38 -04:00
# print('dd')
2019-01-05 09:06:15 -05:00
self.__DB_ERR = ex
return False
try:
2020-07-10 03:57:25 -04:00
myconf = mw.readFile(self.__DB_CNF)
2019-01-05 09:06:15 -05:00
rep = "port\s*=\s*([0-9]+)"
self.__DB_PORT = int(re.search(rep, myconf).groups()[0])
except:
self.__DB_PORT = 3306
2019-01-06 00:12:53 -05:00
# print self.__DB_PASS
2020-07-10 03:57:25 -04:00
#self.__DB_PASS = mw.M('config').where('id=?', (1,)).getField('mysql_root')
2019-01-05 09:06:15 -05:00
try:
self.__DB_CONN = MySQLdb.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS,
port=self.__DB_PORT, charset="utf8", connect_timeout=1, unix_socket=socket)
2021-05-09 12:10:10 -04:00
except MySQLdb.Error as e:
2019-01-05 09:06:15 -05:00
self.__DB_HOST = '127.0.0.1'
self.__DB_CONN = MySQLdb.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS,
port=self.__DB_PORT, charset="utf8", connect_timeout=1, unix_socket=socket)
self.__DB_CUR = self.__DB_CONN.cursor()
return True
2021-05-09 12:08:14 -04:00
except MySQLdb.Error as e:
2019-01-05 09:06:15 -05:00
self.__DB_ERR = e
return False
2021-02-02 08:30:48 -05:00
def setDbConf(self, conf):
self.__DB_CNF = conf
2019-01-06 00:12:53 -05:00
def setPwd(self, pwd):
self.__DB_PASS = pwd
2019-01-06 03:04:06 -05:00
def getPwd(self):
return self.__DB_PASS
2019-01-05 09:06:15 -05:00
def execute(self, sql):
# 执行SQL语句返回受影响行
if not self.__Conn():
return self.__DB_ERR
try:
result = self.__DB_CUR.execute(sql)
self.__DB_CONN.commit()
self.__Close()
return result
2021-05-09 12:08:31 -04:00
except Exception as ex:
2019-01-05 09:06:15 -05:00
return ex
def query(self, sql):
# 执行SQL语句返回数据集
if not self.__Conn():
return self.__DB_ERR
try:
self.__DB_CUR.execute(sql)
result = self.__DB_CUR.fetchall()
# 将元组转换成列表
2021-11-06 07:59:57 -04:00
# data = list(map(list, result))
2019-01-05 09:06:15 -05:00
self.__Close()
2021-11-06 07:59:57 -04:00
return result
2021-05-09 12:08:14 -04:00
except Exception as ex:
2019-01-05 09:06:15 -05:00
return ex
# 关闭连接
def __Close(self):
self.__DB_CUR.close()
self.__DB_CONN.close()