欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SQLAlchemy增删改查

程序员文章站 2024-01-10 14:57:58
...
# flask中使用SQL alchemy------------------------------------------
from flask_sqlalchemy import SQLAlchemy
from flask import Flask


app = Flask(__name__)

# app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:[email protected]:3306/test"

app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + "/home/lmp/sql/first.db"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["SECRET_KEY"] = "sfjlewjlxjfhal234l2jfsldfj"

db = SQLAlchemy(app)

-----------------------------------------------------------------------

# 增
t = Table()    # 模型类实例化
t1 = Table() 
t2 = Table() 
db.session.add(t)   # 增加一条
db.session.commit()

db.session.add_all([t, t1, t2])  # 增加多条
db.sesion.commit()

# 查  -----------------------------------------------------------------
# 查一个 get()
raw = table.query.get(id)

# 查全部 all()
raws = table.query.all()    # 返回列表

# 条件查询 filter()
raws = table.query.filter(table.id>=100)
raw = table.query.filter(table.id>=100).first()

raws = table.query.filter_by(name="xx").all()
raw = table.query.filter_by(name="xx").first()
raw = table.query.filter_by(name="xx").filter(table.id==2)

# 从1查到多
raws = table.query.get(1)   # 返回的是一个列表,列表中的每一元素都是副表的对象
for raw in raws:
      print(raw.field)

# 从多查到1
raw = table.query.filter(grade=="100").first()
print(raw.backref.id)
----------------------------------------------------------------------

# 改
方法一:  
修改一条
change_nums = table.query.get(1).update({"field": "value"})
db.session.commit()

批量修改 
change_nums = table.query.filter(id>10).update({"field": "value"})
db.session.commit()

raws = table.query.filter(id>10).first()
raw.name = "hello"
db.session.commit()
-----------------------------------------------------------------------

# 删除
delete_nums = table.query.filter(id>10).delete() 

-----------------------------------------------------------------------



## 建表
# 学生表
class Student(db.Model):
       __tablename__ = "student"
       id = db.Column(db.Integer, primary_key=True)  # 主键
       name = db.Column(db.String(64), nullable=False)  # 学生姓名  不能为空
       gender = db.Column(db.Enum("男", "女"), nullable=False)  不能为空
       phone = db.Column(db.String(11))  # 手机号   可以为空
       grades = db.relationship("Grade", backref="course")


# 课程表
class Course(db.Model):
       __tablename__ = "course"
      id = db.Column(db.Integer, primary_key=True)  # 主键
      name = db.Column(db.String(64), nullable=False)  # 课程名
       

# 教师表
class Teacher(db.Model):
       __tablename__ = "teacher"
      id = db.Column(db.Integer, primary_key=True)  # 主键
      name = db.Column(db.String(64), nullable=False)  # 老师名
      gender = db.Column(db.Enum("男", "女"), nullable=False)
      phone = db.Column(db.String(11))

# 成绩表
class Grade(db.Model):
       __tablename__ = "grade"
       id = db.Column(db.Integer, primary_key=True)  # 主键
       grade = db.Column(db.String(3), nullable=False)
       student_id = db.Column(db.Integer, db.ForeignKey("student.id"))


相关标签: 工作常见问题

上一篇: MySQL 主从搭建

下一篇: