zoukankan      html  css  js  c++  java
  • 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:123456@127.0.0.1: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:123456@127.0.0.1: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")
    

      

  • 相关阅读:
    APUE.3源码编译(Ubuntu16.04)
    《UNIX环境高级编程》(第三版)阅读笔记---2018-5-9
    css回归之用户界面
    css回归之文本
    js回归之字符串
    js回归之BOM
    js回归之事件
    百度前端面试总结
    书单
    剑指offer做题收获之一:补码
  • 原文地址:https://www.cnblogs.com/52-qq/p/8270431.html
Copyright © 2011-2022 走看看