本文介绍了flask-session:如何创建会话表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用SQLite数据库为我们的应用程序启用服务器端会话。注意我正在使用,它是,更积极的维护。但是,当我使用这两个库时,我遇到了同样的错误。这是我的


$ b

  ,session 
from flask_sqlalchemy import flaskAlchemy $ b $ from flask_sessionstore import Session
$ b app = Flask(__ name__)
app.config ['SQLALCHEMY_DATABASE_URI'] ='sqlite:// //Users/Johnny/DBs/test.db'
app.config ['SQLALCHEMY_TRACK_MODIFICATIONS'] = False#静音警告消息
app.config ['SESSION_TYPE'] ='sqlalchemy'
Session(app)

@ app.route('/')
def index():
return'Hello World!'

if __name__ =='__main__':
app.run(debug = True)

具有 python app.py 的应用程序启动并按预期运行。但是,当我尝试访问索引页时,我得到以下错误:

  sqlalchemy.exc.OperationalError:(sqlite3.OperationalError)no such table:sessions [SQL:'SELECT sessions.id AS sessions_id,sessions.session_id AS sessions_session_id,sessions.data AS sessions_data,sessions.expiry AS sessions_expiry \\\
FROM sessions \\\
WHERE sessions.session_id =?\\\
LIMIT? OFFSET?'] [parameters:('session:xxx ...',1,0)]

看来我需要在运行应用程序之前创建此表。我怎样才能做到这一点?还是我缺少一个配置选项,如果没有找到我创建的表?

解决方案

您将需要提供此您的应用程序配置以及:

pre $ app $ c $ app.config SESSION_SQLALCHEMY_TABLE ='sessions'
app.config [SESSION_SQLALCHEMY] = db

其中 db 是sqlAlchemy实例。所以你应该有这样的东西:

$ $ $ $ $ $ c $ db $ SQLAlchemy($ app $ b $ session $ session
session.app.session_interface.db.create_all()

你可以看看测试例子


I'm trying to enable server-side sessions for my flask application using a SQLite database. Note I'm using Flask-Sessionstore which is a hard fork of Flask-Session and is more actively maintained. However, I'm getting the same error when I use both libraries. Here is my code for app.py:

from flask import Flask, session
from flask_sqlalchemy import SQLAlchemy
from flask_sessionstore import Session

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////Users/Johnny/DBs/test.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False  # quiet warning message
app.config['SESSION_TYPE'] = 'sqlalchemy'
Session(app)

@app.route('/')
def index():
    return 'Hello World!'

if __name__ == '__main__':
    app.run(debug=True)

When I run the application with python app.py everything starts and runs as expected. However, when I try to access the index page at http://127.0.0.1:5000 I get the following error:

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: sessions [SQL: 'SELECT sessions.id AS sessions_id, sessions.session_id AS sessions_session_id, sessions.data AS sessions_data, sessions.expiry AS sessions_expiry \nFROM sessions \nWHERE sessions.session_id = ?\n LIMIT ? OFFSET ?'] [parameters: ('session:xxx...', 1, 0)]

It appears I need to create this table before running the application. How can I do this? Or am I missing a configuration option that creates the table for me if it's not found?

解决方案

You will need to provide this in your app config as well:

app.config[SESSION_SQLALCHEMY_TABLE] = 'sessions'
app.config[SESSION_SQLALCHEMY] = db

where db is sqlAlchemy instance. So you should have something like:

db = SQLAlchemy(app)
session = Session(app)
session.app.session_interface.db.create_all()

You can take a look at the test example of flask_sessionstore on github

这篇关于flask-session:如何创建会话表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 18:18