from sqlalchemy import create_engine, MetaData from sqlalchemy.ext.automap import automap_base from sqlalchemy.orm import Session class SqlAlchemyDB(object): def __init__(self, _dbname, _tables): self.dbName = _dbname print(‘connection.. ‘, self.dbName) connect_string = "mssql+pymssql://user:password@db_url:3433/" + _dbname + "?charset=utf8" self.engine = create_engine(connect_string) self.meatdata = MetaData() self.meatdata.reflect(bind=self.engine, only=_tables) self.base = automap_base(metadata=self.meatdata) self.base.prepare() self.session = Session(self.engine)
原文:https://www.cnblogs.com/412013cl/p/10591869.html