flask框架 資料庫

2021-08-23 14:09:18 字數 4632 閱讀 8512

綜合案例-圖書管理系統

from flask import flask, render_template, flash, redirect

from wtforms import stringfield, submitfield

from flask_wtf import flaskform

from flask_sqlalchemy import sqlalchemy

from wtforms.validators import datarequired

class book(db.model):

id = db.column(db.integer, primary_key=true)

name = db.column(db.string(64))

author_id = db.column(db.integer, db.foreignkey("author.id"))

class author(db.model):

id = db.column(db.integer, primary_key=true)

name = db.column(db.string(64))

books = db.relationship("book", backref="author")

class bookform(flaskform):

book_name = stringfield(label='書籍名:', validators=[datarequired('書籍名不能為空')])

author_name = stringfield(label='作者名:', validators=[datarequired('作者名不能為空')])

submit = submitfield('新增')

def index():

book_from = bookform()

author = author.query.all()

return render_template("book_show.html", book_from=book_from, author=author)

def add_book():

book_form = bookform()

book_name = book_form.book_name.data

author_name = book_form.author_name.data

author = author.query.filter(author.name == author_name).first()

if author:

if not book.query.filter(book.name == book_name,book.author_id == author.id).first():

book = book()

book.name = book_name

book.author_id = author.id

db.session.add(book)

db.session.commit()

flash('新增書籍成功')

else:

flash('新增失敗,該書籍已存在')

else:

author = author()

author.name = author_name

db.session.add(author)

db.session.commit()

book = book()

book.name = book_name

book.author_id = author.id

db.session.add(book)

db.session.commit()

flash('新增作者和書籍成功')

return redirect('/')

if __name__ == "__main__":

# db.drop_all()

db.create_all()

# au1 = author()

# au1.name = '老王'

# au2 = author()

# au2.name = '老李'

# au3 = author()

# au3.name = '老六'

# db.session.add_all([au1, au2, au3])

# db.session.commit()

# book1 = book()

# book1.name = '從前有座山'

# book1.author_id = au1.id

# book2 = book()

# book2.name = '葫蘆娃'

# book2.author_id = au2.id

# book3 = book()

# book3.name = '小金剛'

# book3.author_id = au3.id

# db.session.add_all([book1, book2, book3])

# db.session.commit()

./templates/book_show.html

資料庫遷移(遷移的是資料庫的表結構不包含表的資料)

from flask import flask

from flask_script import manager

from flask_sqlalchemy import sqlalchemy

from flask_migrate import migrate, migratecommand

#使用命令python xx.py db init初始化遷移資料夾 python xx.py db migrate -m '注釋'生成遷移指令碼 python xx.py db upgrade/downgrade 公升級資料庫/降級資料庫

manager.add_command('db', migratecommand)

class student(db.model):

id = db.column(db.integer, primary_key=true)

name = db.column(db.string(64))

age = db.column(db.integer)

def __init__(self, name):

self.name = name

def index():

return "helloworld"

if __name__ == "__main__":

# db.drop_all()

db.create_all()

stu1 = student('zs')

stu2 = student('ls')

stu3 = student('ww')

db.session.add_all([stu1, stu2, stu3])

db.session.commit()

manager.run()

常見的資料庫關係模板

一對多

class book(db.model):

id = db.column(db.integer, primary_key=true)

name = db.column(db.string(64))

author_id = db.column(db.integer, db.foreignkey("author.id"))

class author(db.model):

id = db.column(db.integer, primary_key=true)

name = db.column(db.string(64))

books = db.relationship("book", backref="author")

多對多

student_course = db.table(

"student_course",

db.column("student_id", db.integer, db.foreignkey("student.id")),

db.column("course_id", db.integer, db.foreignkey("course.id"))

)class student(db.model):

id = db.column(db.integer, primary_key=true)

name = db.column(db.string(64))

courses = db.relationship("course", secondary=student_course, backref="student", lazy="dynamic")

def __init__(self, name):

self.name = name

class course(db.model):

id = db.column(db.integer, primary_key=true)

name = db.column(db.string(64))

def __init__(self, name):

self.name = name

flask框架3(資料庫ORM)

flask sqlalchemy 配置 其他配置 使用class user db.model tablename user id db.column db.integer,primary key true,autoincrement true name db.column db.string 50 ...

flask框架 MySQL資料庫基礎配置

1 flask sqlalchemy是乙個為 flask 應用增加 sqlalchemy 支援的擴充套件。它致力於簡化在 flask 中 sqlalchemy 的使用。2 sqlalchemy 是目前python中最強大的 orm框架,功能全面,使用簡單。orm優缺點 優點 1 有語法提示,省去自己...

Flask資料庫遷移

在開發過程中,需要修改資料庫模型,且還要在修改之後更新資料庫。最直接的 式就是刪除 舊表,但這樣會丟失資料。更好的解決辦法是使 資料庫遷移框架,它可以追蹤資料庫模式的變化,然後把變動應 到資料 庫中。在flask中可以使 flask migrate擴充套件,來實現資料遷移。並且整合到flask sc...