flask-sqlalchemy基本操作数据库

Posted 不会起名字

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了flask-sqlalchemy基本操作数据库相关的知识,希望对你有一定的参考价值。

# -*- coding: utf-8 -*-
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column,Integer,String,Text,ForeignKey,DateTime,UniqueConstraint,Index
from sqlalchemy import create_engine
from flask import Blueprint,render_template,redirect,session

login = Blueprint("login",__name__)

Base = declarative_base()

class Users(Base):
    __tablename__ = "users"

    id = Column(Integer,primary_key=Text,autoincrement=True)
    name = Column(String(32),index=True,nullable=False)

def init_db():
    engine = create_engine(
        "mysql+pymysql://root:[email protected]:3306/flask?charset=utf8",
        max_overflow = 0,
        pool_size = 5,
        pool_timeout = 30,
        pool_recycle = -1
    )
    Base.metadata.create_all(engine)

def drop_db():
    engine = create_engine(
        "mysql+pymysql://root:[email protected]:3306/flask?charset=utf8",
        max_overflow = 0,
        pool_size = 5,
        pool_timeout = 30,
        pool_recycle = -1
    )
    Base.metadata.drop_all(engine)


if __name__ == ‘__main__‘:
    # init_db()
    drop_db()



@login.route("/login")
def login_s():
    session["user"] = "张大爷"
    return render_template("login.html")

  

以上是关于flask-sqlalchemy基本操作数据库的主要内容,如果未能解决你的问题,请参考以下文章

那些年我们学Flask-SQLAlchemy,实现数据库操作,分页等功能

flask-sqlalchemy基本操作数据库

Flask-SQLAlchemy操作

Flask-SQLAlchemy 学习总结

使用Flask-SQLALchemy操作MySQL数据库

Flask-SQLAlchemy安装及设置