|
@@ -0,0 +1,31 @@
|
|
|
+from sqlalchemy import create_engine, MetaData
|
|
|
+from sqlalchemy.orm import sessionmaker, scoped_session
|
|
|
+from config import using_config
|
|
|
+
|
|
|
+
|
|
|
+class DB():
|
|
|
+ def __init__(self, config=None):
|
|
|
+ self.config = using_config if not config else config
|
|
|
+ self.engine = self.DBengine()
|
|
|
+ self.session = self.DBSession()
|
|
|
+ self.metadata = MetaData(self.engine)
|
|
|
+
|
|
|
+ def DBengine(self):
|
|
|
+ # 初始化数据库连接:
|
|
|
+ db_uri = 'mysql+pymysql://' \
|
|
|
+ '{username}:{password}@{host}:{port}/{database}'.format(username=self.config['username'],
|
|
|
+ password=self.config['password'],
|
|
|
+ host=self.config['host'],
|
|
|
+ port=self.config['port'],
|
|
|
+ database=self.config['database'])
|
|
|
+
|
|
|
+ engine = create_engine(db_uri, pool_size=1024, pool_recycle=1800,
|
|
|
+ pool_pre_ping=True, max_overflow=100, echo=False)
|
|
|
+ return engine
|
|
|
+
|
|
|
+ def DBSession(self):
|
|
|
+ # 创建DBSession类型:
|
|
|
+ session_factory = sessionmaker(bind=self.engine)
|
|
|
+ DBSession = scoped_session(session_factory)
|
|
|
+ DBSession = DBSession()
|
|
|
+ return DBSession
|