sqlalchemy的使用

from sqlalchemy import Column, String, create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base


# 创建模型的基类
Base = declarative_base()


class User(Base):
    __tablename__ = "user"
    id = Column(String(32), primary_key=True)
    name = Column(String(32))
    batch = Column(String(32), default=None)


engine = create_engine('mysql+pymysql://root:[email protected]:8001/test')

# 创建DBSession类型
DBSession = sessionmaker(bind=engine)

# 创建session对象
session = DBSession()

new_user = User(id='4', name='kzzf1')
session.add(new_user)

query = session.query(User).filter(User.name.in_(['kzzf', 'sk']))
for user in query:
    user.batch = 5

session.commit()
session.close()

你可能感兴趣的:(python)