from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
db = SQLAlchemy()
class Student(db.Model):
# 自增主鍵 id 字段
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
s_name = db.Column(db.String(10), unique=True, nullable=False)
s_age = db.Column(db.Integer, default=18)
# auto_now_add
# auto_now 只在save()方法調用的時候auto_now字段才做更新诵叁。
# update() 方法調用時auto_now字段不跟新
create_time = db.Column(db.DateTime, default=datetime.now)
# 如果沒定義tablename拌牲,那么名字為模型名稱小寫
# 定義tablename 表示模型遷移導數(shù)據(jù)庫中對應的表名稱
__tablename__ = 'stu'
def save(self):
# 事務session的add方法召廷,其實是準備插入語句挤忙,insert
db.session.add(self)
# 事務session提交了输涕,數(shù)據(jù)才會插入到數(shù)據(jù)庫中
db.session.commit()
import redis
from flask import Flask
from flask_script import Manager
from flask_session import Session
# from sqlalchemy.sql.functions import user
import user
from user.models import db
from user.views import blue
app = Flask(__name__)
# 設置secret_key
app.secret_key = 'o[ayfosjnfhw487-q9ruq[aek'
# 設置flask-session的內容柜思,將session數(shù)據(jù)保存在redis中
app.config['SESSION_TYPE'] = 'redis'
# redis.Redis(host='127.0.0.1', port=6379, password='密碼')
app.config['SESSION_REDIS'] = redis.Redis(host='47.103.14.109', port=6379, password='caozhihua')
# 初始化Session的兩種方式:
# 第一種方式
Session(app)
# 第二種方式
# sess = Session()
# sess.init_app(app)
# 第三步: 注冊藍圖
app.register_blueprint(blueprint=blue, url_prefix='/user')
# 數(shù)據(jù)庫的配置
#mysql+pymysql://root:password@47.103.14.109:3306/flask1901
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:123456@47.103.14.109:3306/flask1901'
db.init_app(app)
if __name__ == '__main__':
manage = Manager(app)
manage.run()