【发布时间】:2023-01-14 00:28:48
【问题描述】:
我目前正在使用 flask 和 flask_sqlalchemy 在 python 中编写程序。我已经完成了与文档中相同的步骤。但是当数据库自动创建时,它有一个未知的文件类型,尽管它应该是一个 sqlite 数据库。顺便说一句,我正在使用 Pycharm。
from flask import Flask, render_template, request, redirect
from flask_sqlalchemy import SQLAlchemy
# create the extension
db = SQLAlchemy()
# create the app
app = Flask(__name__)
# configure the SQLite database, relative to the app instance folder
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///filemanager.db"
# initialize the app with the extension
db.init_app(app)
class File(db.Model):
id = db.Column(db.Integer, primary_key=True)
path = db.Column(db.String, unique=True, nullable=False)
type = db.Column(db.String, unique=False, nullable=False)
@app.route('/', methods=['GET', 'POST'])
def index():
return render_template('index.html')
if __name__ == '__main__':
create_database = True
if create_database:
with app.app_context():
db.create_all()
app.run(debug=True, port=5008)
我尝试手动将文件类型更改为 sqlite,但它仍然不包含表和列。如果我在控制台中手动创建列,一切正常,但我已经并且想要以编程方式进行。 我还尝试了另一个 stackoverflow 答案,他将代码分成两个文件,但没有成功。 提前致谢!
【问题讨论】:
-
无法重现,运行上面的命令会创建一个类型为
instance/filemanager.db: SQLite 3.x database, last written using SQLite version 3032003的 db 文件。 -
@ljmc 数据库包含一个名为“文件”的列?
-
它包含一个名为
file的表(使用sqlite> .tables)。 -
“它有一个未知的文件类型”你在哪里看到这个?
标签: python sqlite flask-sqlalchemy