from sqlalchemy import Column, Integer, String
from untils.conn import Base
class Student(Base):
id = Column(Integer, primary_key=True, autoincrement=True)
s_name = Column(String(10), unique = True, nullable = False)
s_age = Column(Integer, default = 18)
__tablename__ = 'student'
#连接数据库
#mysqp+pymysql://root:12345@127.0.0.1:3306/tornado9
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
db_url = 'mysql+pymysql://root:DENG5rong2hua0!@127.0.0.1:3306/tornado9'
#创建连接
engine = create_engine(db_url)
#模型与数据库关联的基类,模型必须继承Base
Base = declarative_base()
#创建会话(session)
DbSession = sessionmaker(bind = engine)
session = DbSession
1.模型定义:必须继承Base= declarative_base(bind=engine)
2.数据库连接配置:db_url = 'mysql+pymysql://root:DENG5rong2hua0!@127.0.0.1:3306/database'
3.创建连接,:create_engine(连接地址)
4.创建会话对象: DbSession = sessionmaker(bind = engine)
session = DbSession()
增session.add(对象) session.commit()
sessio.add_all().session.commit()
删session.delete() .session.commit()
查filter(模型名,字段==值)
,filter(字段=值)
改filter(条件).update({'字段1':值,‘字段2’:值...})