码迷,mamicode.com
首页 > 数据库 > 详细

python基于SQLAchemy的DBtools

时间:2017-12-21 11:47:53      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:imp   pass   from   color   port   make   mysql   post   turn   

代码如下:

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)

 

python基于SQLAchemy的DBtools

标签:imp   pass   from   color   port   make   mysql   post   turn   

原文地址:http://www.cnblogs.com/zipon/p/8078754.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!