panelMysql.py 3.76 KB
Newer Older
jose's avatar
jose committed
1 2 3 4 5 6
#coding: utf-8
# +-------------------------------------------------------------------
# | 宝塔Linux面板
# +-------------------------------------------------------------------
# | Copyright (c) 2015-2099 宝塔软件(http://bt.cn) All rights reserved.
# +-------------------------------------------------------------------
7
# | Author: hwliang <hwl@bt.cn>
jose's avatar
jose committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# +-------------------------------------------------------------------

import re,os,sys,public

class panelMysql:
    __DB_PASS = None
    __DB_USER = 'root'
    __DB_PORT = 3306
    __DB_HOST = 'localhost'
    __DB_CONN = None
    __DB_CUR  = None
    __DB_ERR  = None
    __DB_NET = None
    #连接MYSQL数据库
    def __Conn(self):
        if self.__DB_NET: return True
        try:
25
            socket = '/tmp/mysql.sock'
jose's avatar
jose committed
26 27
            try:
                if sys.version_info[0] != 2:
28 29 30 31 32
                    try:
                        import pymysql
                    except:
                        public.ExecShell("pip install pymysql")
                        import pymysql
jose's avatar
jose committed
33 34 35 36 37 38 39 40 41 42 43
                    pymysql.install_as_MySQLdb()
                import MySQLdb
                if sys.version_info[0] == 2:
                    reload(MySQLdb)
            except Exception as ex:
                try:
                    import pymysql
                    pymysql.install_as_MySQLdb()
                    import MySQLdb
                except Exception as e:
                    self.__DB_ERR = e
44
                    return False
jose's avatar
jose committed
45
            try:
46 47 48
                myconf = public.readFile('/etc/my.cnf')
                rep = r"port\s*=\s*([0-9]+)"
                self.__DB_PORT = int(re.search(rep,myconf).groups()[0])
jose's avatar
jose committed
49
            except:
50 51
                self.__DB_PORT = 3306
            self.__DB_PASS = public.M('config').where('id=?',(1,)).getField('mysql_root')
jose's avatar
jose committed
52 53 54 55
            
            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)
            except MySQLdb.Error as e:
56
                self.__DB_HOST = '127.0.0.1'
jose's avatar
jose committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
                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
        except MySQLdb.Error as e:
            self.__DB_ERR = e
            return False

    #连接远程数据库
    def connect_network(self,host,port,username,password):
        self.__DB_NET = True
        try:
            self.__DB_CONN = MySQLdb.connect(host = host,user = username,passwd = password,port = port,charset="utf8",connect_timeout=10)
            self.__DB_CUR  = self.__DB_CONN.cursor()
        except MySQLdb.Error as e:
            self.__DB_ERR = e
            return False

    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
        except Exception as ex:
            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()
            #将元组转换成列表
jose's avatar
jose committed
93 94 95 96
            if sys.version_info[0] == 2:
                data = map(list,result)
            else:
                data = list(map(list,result))
jose's avatar
jose committed
97 98 99 100
            self.__Close()
            return data
        except Exception as ex:
            return ex
101

jose's avatar
jose committed
102 103 104 105
    #关闭连接        
    def __Close(self):
        self.__DB_CUR.close()
        self.__DB_CONN.close()