赞
踩
from flask import Flask # 1.导入模块 from flask_sqlalchemy import SQLAlchemy app= Flask(__name__) import os # 获取绝对路径 BASE_DIR=os.path.abspath(os.path.dirname(__file__)) print(BASE_DIR) # # 2.配置数据库连接 class Config(object): # 连接数据库 SQLALCHEMY_DATABASE_URI = 'sqlite:///'+os.path.join(BASE_DIR,'fk.sqlite') # 设置sqlalchemy 自动更新 SQLALCHEMY_TRACK_MODIFICATIONS = True # 配置 SECRET_KEY SECRET_KEY = 'abev' app.config.from_object(Config) # 3.创建sqlalchemy对象 db=SQLAlchemy(app) # 4.创建数据模型,一对多 # 学科 # 继承db.Model class Subject(db.Model): __tablename__='tb_subject'#设置表格名称 id=db.Column(db.Integer,primary_key= True) #设置主键 name=db.Column(db.String(32),nullable=False) #设置名称 # 定义关系字段,不会再表格中存在,只是为了方便查询 student=db.relationship('Student',backref='subject') def __repr__(self): return '<object name:{}>'.format(self.name) # 学生 class Student(db.Model): __tablename__ = 'tb_student' # 设置表格名称 id = db.Column(db.Integer, primary_key=True) # 设置主键 name = db.Column(db.String(32), nullable=False) # 设置名称 subject_id=db.Column(db.Integer,db.ForeignKey('tb_subject.id')) #设置外键 def __repr__(self): return '<object name:{}>'.format(self.name) if __name__ == '__main__': import pymysql pymysql.install_as_MySQLdb() db.drop_all() #删除所有的表格 db.create_all() #创建所有的表格 # 添加数据: #1.创建对象 subject1=Subject(name='python') subject2=Subject(name='java') db.session.add(subject1) db.session.add(subject2) db.session.commit() #默认开启事务,所以要提交 #2.创建学生 std1=Student(name='zs',subject_id=subject1.id) std2=Student(name='ls',subject_id=subject1.id) std3=Student(name='ww',subject_id=subject2.id) std4 = Student(name='ss', subject_id=subject2.id) db.session.add_all([std1,std2,std3,std4]) db.session.commit() # 查询 # 1.查询所有的学科 # ret=Subject.query.all() # print(ret) # [<object name:python>, <object name:java>] # 2. first() 获取第一条记录 # ret=Subject.query.first() # print(ret) # < objectname: python > # 3.get():必须是 唯一标识 # ret=Subject.query.get(2) # print(ret) # < objectname: python > # 4.filter_by # ret=Student.query.filter_by(subject_id=1).all() # print(ret) # [<object name:zs>, <object name:ls>] # ret=Student.query.filter_by(subject_id=1,name='zs').all() # print(ret) # [<object name:zs>] # 5.filter: 参数 必须是 类名.属性名=='xxx' # ret =Student.query.filter(Student.subject_id==1).all() # print(ret) # [<object name:zs>, <object name:ls>] # 6. or # from sqlalchemy import or_ # ret =Student.query.filter(or_(Student.subject_id==1,Student.name=='ww')).all() # print(ret) #[<object name:zs>, <object name:ls>, <object name:ww>] # 7.select * from limit 2,3; # offset() 偏移 # ret=Student.query.offset(2).all() # print(ret) # 8.limit() # ret = Student.query.offset(2).limit(1).all() # print(ret) # 9.关联查询 # 查询python学科对应的学生 sub_obj =Subject.query.filter_by(name='python').first() # print(sub_obj) # print(sub_obj.name) print(sub_obj.student) # <object name:python> # python # [<object name:zs>, <object name:ls>] # 不使用关系属性字段 sub_obj =Subject.query.filter_by(name='python').first() ret =Student.query.filter_by(subject_id=sub_obj.id).all() print(ret) #[<object name:zs>, <object name:ls>] # 查询张三对应的学科 # Student_obj=Student.query.filter_by(name='zs').first() # ret=Subject.query.get(Student_obj.subject_id) # print(ret) # <object name:python> # 使用关系字段 # student_obj=Student.query.filter_by(name='zs').first() # print(student_obj.subject) # < objectname: python > #修改 #将 id=3 改成www # Student.query.filter_by(id=3).update({'name':'wwww'}) # db.session.commit() #或者使用get #student_obj=Student.query.get(3) #student_obj.name='www' #db.session.add(student_obj) #db.session.commit() # 删除操作 # 删除 id=3 的对象 # student_obj=Student.query.get(3) # db.session.delete(student_obj) # db.session.commit() # app.run(debug=True) #此后的代码都不执行
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。