建表插入数据:
import sqlalchemy from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer,String,Date from sqlalchemy.orm import sessionmaker engine = create_engine("mysql+pymysql://tim:[email protected]/test", encoding = "utf-8",echo=True) Base = declarative_base() class User(Base): __tablename__= "user" id = Column(Integer,primary_key=True) name = Column(String(32)) password = Column(String(64)) Base.metadata.create_all(engine) #创建表结构 Session_class = sessionmaker(bind=engine) #创建与数据库的会话session class,注意这里返回给session的是个类,不是实例 Session = Session_class() #生成session实例 user_obj = User(name=‘tim‘,password=‘sina4433‘) #生成你要创建的数据对象 user_obj2 = User(name = ‘jack‘,password=‘abc‘) print(user_obj.name,user_obj.id) #此时还没创建对象,不信打印一下id,还是None Session.add(user_obj) # 把要创建的数据对象添加到这个session里,一会儿统一创建 Session.add(user_obj2) print(user_obj,user_obj.id) #此时也依然还没创建 Session.commit()
查询:
import sqlalchemy from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer,String,Date from sqlalchemy.orm import sessionmaker engine = create_engine("mysql+pymysql://tim:[email protected]/test", encoding = "utf-8") Base = declarative_base() class User(Base): __tablename__= "user" id = Column(Integer,primary_key=True) name = Column(String(32)) password = Column(String(64)) def __repr__(self): return "<%s name:%s>" % (self.id,self.name) Base.metadata.create_all(engine) #创建表结构 Session_class = sessionmaker(bind=engine) #创建与数据库的会话session class,注意这里返回给session的是个类,不是实例 Session = Session_class() #生成session实例 # user_obj = User(name=‘tim‘,password=‘sina4433‘) #生成你要创建的数据对象 # user_obj2 = User(name = ‘jack‘,password=‘abc‘) # print(user_obj.name,user_obj.id) #此时还没创建对象,不信打印一下id,还是None # # Session.add(user_obj) # 把要创建的数据对象添加到这个session里,一会儿统一创建 # Session.add(user_obj2) # print(user_obj,user_obj.id) #此时也依然还没创建 # Session.commit() # data = Session.query(User).filter_by().all() #结果是一个列表 # print(data) # data = Session.query(User).filter_by(name=‘tim‘).all() #结果是一个列表 # print(data[0].id) # data = Session.query(User).filter_by(id=1).all() # print(data) # data = Session.query(User).filter(User.id>1).all() # print(data) # data = Session.query(User).filter(User.id>1).filter(User.id<4).all() # print(data) data = Session.query(User).filter(User.id>1).filter(User.id<4).first() print(data) # #更改数据 # data.name = "zhang" # data.password = "Shit" # Session.commit() # my_user = Session.query(User).filter_by(id=1).first() # my_user.name = ‘Jack‘ # # fake_user = User(name=‘Rain‘,password=‘123456‘) # # Session.add(fake_user) # # print(Session.query(User).filter(User.name.in_([‘Jack‘,‘rain‘])).all()) # # Session.rollback() # # print(Session.query(User).filter(User.name.in_([‘Jack‘,‘rain‘])).all())
多表连接:
import sqlalchemy from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer,String,Enum,ForeignKey,DATE from sqlalchemy.orm import sessionmaker,relationship engine = create_engine("mysql+pymysql://tim:[email protected]/test", encoding = "utf-8") Base = declarative_base() class Student(Base): __tablename__= "student" id = Column(Integer,primary_key=True,autoincrement=True) name = Column(String(32),nullable=True) register_date = Column(DATE,nullable=True) #加个关联关系 def __repr__(self): return "<%s name:%s>"%(self.id,self.name) class StudyRecord(Base): __tablename__ = "StudyRecord" id = Column(Integer,primary_key=True,autoincrement=True) day = Column(Integer) status = Column(String(32)) stu_id = Column(Integer,ForeignKey(Student.id)) #加个关联关系 student = relationship("Student",backref="my_study_record") def __repr__(self): return "<%s day:%s status:%s>"%(self.student.name,self.day,self.status) Base.metadata.create_all(engine) #创建表结构 Session_class = sessionmaker(bind=engine) #创建与数据库的会话session class,注意这里返回给session的是个类,不是实例 session = Session_class() #生成session实例 # ret = Session.query(Student).join(Study_record).all() #这种方式需要主外键关联 # print(ret) # ret = session.query(Student,StudyRecord).filter(Student.id==StudyRecord.stu_id).all() # print(ret) # s1 = Student(name="Alex",register_date=‘2013-02-01‘) # s2 = Student(name="Jack",register_date=‘2014-04-01‘) # s3 = Student(name="gao",register_date=‘2013-10-01‘) # s4 = Student(name="wang",register_date=‘2011-11-01‘) # # study_obj1 = StudyRecord(day=1,status="YES",stu_id=1) # study_obj2 = StudyRecord(day=2,status="NO",stu_id=1) # study_obj3 = StudyRecord(day=3,status="YES",stu_id=1) # study_obj4 = StudyRecord(day=1,status="YES",stu_id=2) # # session.add_all([s1,s2,s3,s4,study_obj1,study_obj2,study_obj3,study_obj4]) # session.commit() #查出Alex上了几节课 stu_obj = session.query(Student).filter(Student.name==‘Alex‘).first() print(stu_obj.my_study_record)
时间: 2024-10-10 01:44:00