代码如下:
from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker BIZ_DB = {"dbusr":"test","dbpasswd":"123456","dburl":"127.0.0.1:3306","dbname":"test"} OFFLINE_DB = {"dbusr":"test","dbpasswd":"123456","dburl":"127.0.0.1:3306","dbname":"test"} def create_session(dbusr,dbpasswd,dburl,dbname): engine = create_engine("mysql+pymysql://{}:{}@{}/{}".format(dbusr,dbpasswd,dburl,dbname)) DBsession = sessionmaker(bind=engine) session = DBsession() return session def create_session_biz(): dbusr = BIZ_DB["dbusr"] dbpasswd = BIZ_DB["dbpasswd"] dburl = BIZ_DB["dburl"] dbname = BIZ_DB["dbname"] return create_session(dbusr,dbpasswd,dburl,dbname) def create_session_offline(): dbusr = OFFLINE_DB["dbusr"] dbpasswd = OFFLINE_DB["dbpasswd"] dburl = OFFLINE_DB["dburl"] dbname = OFFLINE_DB["dbname"] return create_session(dbusr, dbpasswd, dburl, dbname)