赞
踩
Base = declarative_base()
engine = create_engine('mysql+mysqlconnector://root:password@localhost:3306/test')
DBSession = sessionmaker(bind=engine)
dbseesion = scoped_session(DBseesion)
from sqlalchemy import create_engine, MetaData, Table from sqlalchemy.orm import sessionmaker, scoped_session from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() # 模型类继承 # 创建引擎 engine = create_engine("mysql+pymysql://root:root@localhost/woniunote", echo=False) DBseesion = sessionmaker(bind=engine) # 定义模型类继承父类及数据连接会话 dbseesion = scoped_session(DBseesion) # 线程安全 md = MetaData(bind=engine) # 元数据 自动加载表结构,把表结构的类自动映射为类属性 #该Users表以提前创建,只需要引用,未使用sqlalchemy创建 class Users(Base): __table__ = Table('users', md, autoload=True) class Article(Base): __table__ = Table('article', MetaData(bind=engine), autoload=True)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。