python基于SQLAchemy的DBtools

Posted zipon

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python基于SQLAchemy的DBtools相关的知识,希望对你有一定的参考价值。

代码如下:

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的主要内容,如果未能解决你的问题,请参考以下文章

python操作mysql ------- SqlAchemy正传

python SQLAchemy多外键关联

mysql数据库----python操作mysql ------pymysql和SQLAchemy

2Python全栈之路系列之SQLAchemy

python教程18python操作Mysql,pymysql,SQLAchemy

Python3 之 MySQL操作模块:pymsql和sqlachemy