models.pyhtml
from flask_sqlalchemy import SQLAlchemy db=SQLAlchemy() class Users(db.Model): __tablename__ = "users_flask" id = db.Column(db.Integer,primary_key=True) name = db.Column(db.String(32),nullable=False) if __name__ == '__main__': from app01 import create_app app=create_app() db.create_all(app=app) # db.drop_all(app=app)
__init__.pymysql
from flask import Flask from app01.models import db from app01.views import user def create_app(): app = Flask(__name__) app.config["DEBUG"] = True app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:123@127.0.0.1:3306/my_sqlalchemy?charset=utf8" app.config["SQLALCHEMY_POOL_SIZE"] = 50 app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db.init_app(app) app.register_blueprint(user.user_bp) return app
user.pysql
from flask import Blueprint from app01.models import Users,db user_bp = Blueprint("user_bp",__name__) @user_bp.route("/add_user/<username>",methods=["POST","GET"]) def add_user(username): u = Users(name=username) db.session.add(u) db.session.commit() return "200 ok I am user_bp" @user_bp.route("/get_user/<username>",methods=["POST","GET"]) def get_user(username): u = Users.query.filter(Users.name == username).first() return str(u.id)
manage.pyflask
from app01 import create_app app = create_app() if __name__ == '__main__': app.run()
參考 :http://www.javashuo.com/article/p-qjkrfexr-v.htmlsession
from flask import Flaskapp
# 導入Flask-SQLAlchemy中的SQLAlchemy
from flask_sqlalchemy import SQLAlchemy函數
# 實例化SQLAlchemy
db = SQLAlchemy()
# PS : 實例化SQLAlchemy的代碼必需要在引入藍圖以前spa
from .views.users import usercode
def create_app():
app = Flask(__name__)orm
# 初始化App配置 這個app配置就厲害了,專門針對 SQLAlchemy 進行配置
# SQLALCHEMY_DATABASE_URI 配置 SQLAlchemy 的連接字符串兒
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:DragonFire@127.0.0.1:3306/dragon?charset=utf8"
# SQLALCHEMY_POOL_SIZE 配置 SQLAlchemy 的鏈接池大小
app.config["SQLALCHEMY_POOL_SIZE"] = 5
# SQLALCHEMY_POOL_TIMEOUT 配置 SQLAlchemy 的鏈接超時時間
app.config["SQLALCHEMY_POOL_TIMEOUT"] = 15
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
# 初始化SQLAlchemy , 本質就是將以上的配置讀取出來
db.init_app(app)
app.register_blueprint(user)
return app
MyApp/__init__.py
from MyApp import db Base = db.Model # 這句話你是否還記的? # from sqlalchemy.ext.declarative import declarative_base # Base = declarative_base() # 每一次咱們在建立數據表的時候都要作這樣一件事 # 然而Flask-SQLAlchemy已經爲咱們把 Base 封裝好了 # 創建User數據表 class Users(Base): # Base實際上就是 db.Model __tablename__ = "users" __table_args__ = {"useexisting": True} # 在SQLAlchemy 中咱們是導入了Column和數據類型 Integer 在這裏 # 就和db.Model同樣,已經封裝好了 id = db.Column(db.Integer,primary_key=True) username = db.Column(db.String(32)) password = db.Column(db.String(32)) if __name__ == '__main__': from MyApp import create_app app = create_app() # 這裏你要回顧一下Flask應該上下文管理了 # 離線腳本: with app.app_context(): db.drop_all() db.create_all() MyApp/models.py
from flask import Blueprint, request, render_template user = Blueprint("user", __name__) from MyApp.models import Users from MyApp import db @user.route("/login",methods=["POST","GET"]) def user_login(): if request.method == "POST": username = request.form.get("username") password = request.form.get("password") # 還記不記得咱們的 # from sqlalchemy.orm import sessionmaker # Session = sessionmaker(engine) # db_sesson = Session() # 如今不用了,由於 Flask-SQLAlchemy 也已經爲咱們作好會話打開的工做 # 咱們在這裏作個弊: db.session.add(Users(username=username,password=password)) db.session.commit() # 而後再查詢,捏哈哈哈哈哈 user_info = Users.query.filter(Users.username == username and User.password == password).first() print(user_info.username) if user_info: return f"登陸成功{user_info.username}" return render_template("login.html") MyApp/views/user.py