|
|
@@ -0,0 +1,35 @@
|
|
|
+#!/usr/bin/env python3
|
|
|
+# -*- coding:utf-8 -*-
|
|
|
+from sqlalchemy import create_engine, text
|
|
|
+from sqlalchemy.dialects.mysql import pymysql
|
|
|
+from sqlalchemy.orm import sessionmaker
|
|
|
+from sqlalchemy.ext.declarative import declarative_base
|
|
|
+from config import database_config
|
|
|
+
|
|
|
+
|
|
|
+class Mysql(object):
|
|
|
+ def __init__(self):
|
|
|
+ host = database_config.MYSQL_HOST
|
|
|
+ port = database_config.MYSQL_PORT
|
|
|
+ user = database_config.MYSQL_USER
|
|
|
+ passwd = database_config.MYSQL_PASSWD
|
|
|
+ dbname = database_config.MYSQL_DB
|
|
|
+ self.engine = self._connect(host, port, user, passwd, dbname)
|
|
|
+ self.engine = create_engine(self.engine)
|
|
|
+ self._DBSession = sessionmaker(bind=self.engine)
|
|
|
+
|
|
|
+ def _connect(self, host, port, user, pwd, db):
|
|
|
+ client = "mysql+pymysql://" + user + ":" + pwd + "@" + host + ":" + str(port) + "/" + db
|
|
|
+ return client
|
|
|
+
|
|
|
+ @property
|
|
|
+ def DBSession(self):
|
|
|
+ return self._DBSession
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ client = Mysql()
|
|
|
+ sess = client.DBSession()
|
|
|
+ info = sess.execute(text('show tables'))
|
|
|
+ for tb in info:
|
|
|
+ print(tb)
|