Python 之 sqlalchemy删除数据
#Author Kangimport sqlalchemyfrom sqlalchemy import create_enginefrom sqlalchemy.ext.declarative import declarative_basefrom sqlalchemy import Column,Integer,Stringfrom sqlalchemy.orm import sessionmaker#创建对象的基类:Base = declarative_base()#定义User对象:class User(Base): #表的名字: __tablename__ = 'user' #表的结构: userid = Column(Integer,primary_key=True) username = Column(String(20)) age = Column(Integer) department = Column(String(20))#初始化数据库连接engine = create_engine("mysql+pymysql://kang:123456@10.3.152.35/test",encoding="utf-8")#创建session类型DBSession = sessionmaker(bind=engine)#创建session对象session = DBSession()#先查询,后删除session.query(User).filter(User.username == "Jack").delete()session.commit()session.close()
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。