From 60e8de84c4cdcb3520052755a0be22db07535d72 Mon Sep 17 00:00:00 2001 From: midoks Date: Tue, 2 Aug 2022 00:57:05 +0800 Subject: [PATCH] =?UTF-8?q?maridb=20=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- class/core/mw.py | 10 +++++ class/plugin/mariaDb.py | 93 ++++++++++++++++++++++++++++++++++++++++ class/plugin/orm.py | 4 +- plugins/mariadb/index.py | 4 +- scripts/lib.sh | 6 +-- 5 files changed, 109 insertions(+), 8 deletions(-) create mode 100755 class/plugin/mariaDb.py diff --git a/class/core/mw.py b/class/core/mw.py index 6ca763223..410675886 100755 --- a/class/core/mw.py +++ b/class/core/mw.py @@ -1044,3 +1044,13 @@ def getMyORMDb(): import ormDb o = ormDb.ORM() return o + + +def getMyORMMariaDb(): + ''' + 获取MariaDB资源的ORM | pip install mariadb + ''' + sys.path.append(os.getcwd() + "/class/plugin") + import mariaDb + o = mariaDb.ORM() + return o diff --git a/class/plugin/mariaDb.py b/class/plugin/mariaDb.py new file mode 100755 index 000000000..9e1920acc --- /dev/null +++ b/class/plugin/mariaDb.py @@ -0,0 +1,93 @@ +# coding: utf-8 + +import re +import os +import sys + + +class ORM: + __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' + + __DB_SOCKET = '/www/server/mysql/mysql.sock' + __DB_CHARSET = 'utf8' + + def __Conn(self): + '''连接MariaDB数据库''' + try: + + try: + import mariadb + except Exception as ex: + self.__DB_ERR = ex + return False + + try: + self.__DB_CONN = mariadb.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS, + port=int(self.__DB_PORT), charset=self.__DB_CHARSET, connect_timeout=1) + except Exception as e: + self.__DB_HOST = '127.0.0.1' + self.__DB_CONN = mariadb.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS, + port=int(self.__DB_PORT), charset=self.__DB_CHARSET, connect_timeout=1) + + self.__DB_CUR = self.__DB_CONN.cursor() + + return True + except Exception as e: + self.__DB_ERR = e + return False + + def setDbConf(self, conf): + self.__DB_CNF = conf + + def setSocket(self, sock): + self.__DB_SOCKET = sock + + def setCharset(self, charset): + self.__DB_CHARSET = charset + + def setPort(self, port): + self.__DB_PORT = port + + def setPwd(self, pwd): + self.__DB_PASS = pwd + + def getPwd(self): + return self.__DB_PASS + + 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() + # 将元组转换成列表 + data = map(list, result) + self.__Close() + return data + except Exception as ex: + return ex + + # 关闭连接 + def __Close(self): + self.__DB_CUR.close() + self.__DB_CONN.close() diff --git a/class/plugin/orm.py b/class/plugin/orm.py index ab1951bbe..7dc75261c 100755 --- a/class/plugin/orm.py +++ b/class/plugin/orm.py @@ -85,9 +85,9 @@ class ORM: self.__DB_CUR.execute(sql) result = self.__DB_CUR.fetchall() # 将元组转换成列表 - # data = list(map(list, result)) + data = map(list, result) self.__Close() - return result + return data except Exception as ex: return ex diff --git a/plugins/mariadb/index.py b/plugins/mariadb/index.py index c29abb80a..e6d93b993 100755 --- a/plugins/mariadb/index.py +++ b/plugins/mariadb/index.py @@ -145,9 +145,9 @@ def pSqliteDb(dbname='databases'): def pMysqlDb(): # mysql.connector - db = mw.getMyORM() + # db = mw.getMyORM() # MySQLdb | - # db = mw.getMyORMDb() + db = mw.getMyORMMariaDb() db.setDbConf(getConf()) db.setPort(getDbPort()) diff --git a/scripts/lib.sh b/scripts/lib.sh index 6535b65aa..48f889941 100755 --- a/scripts/lib.sh +++ b/scripts/lib.sh @@ -113,9 +113,8 @@ cd /www/server/mdserver-web && pip3 install -r /www/server/mdserver-web/requirem pip3 install gevent-websocket==0.10.1 pip3 install flask-caching==1.10.1 -pip3 install flask-session==0.3.2 -pip3 install flask-socketio==5.2.0 pip3 install mysqlclient +pip3 install mariadb if [ ! -f /www/server/mdserver-web/bin/activate ];then @@ -131,7 +130,6 @@ pip3 install -r /www/server/mdserver-web/requirements.txt pip3 install gevent-websocket==0.10.1 pip3 install flask-caching==1.10.1 -pip3 install flask-session==0.3.2 -pip3 install flask-socketio==5.2.0 pip3 install mysqlclient +pip3 install mariadb