码迷,mamicode.com
首页 > 数据库 > 详细

flask-sqlalchemy基本操作数据库

时间:2018-01-11 22:25:17      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:string   metadata   date   log   coding   turn   name   ssi   red   

# -*- 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")

  

flask-sqlalchemy基本操作数据库

标签:string   metadata   date   log   coding   turn   name   ssi   red   

原文地址:https://www.cnblogs.com/52-qq/p/8270431.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!