【问题标题】:sqlalchemy.exc.OperationalErrorsqlalchemy.exc.OperationalError
【发布时间】:2019-08-29 06:56:38
【问题描述】:

我正在 youtube 上做一个烧瓶教程。但是我总是得到这个错误。我什至复制了他的代码并在我的电脑上尝试过,但它仍然会引发该错误。

from flask import Flask, render_template, url_for, request, redirect
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///test.db"
db = SQLAlchemy(app)


class Todo(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.String(200), nullable=False)
    date_created = db.Column(db.DateTime, default=datetime.utcnow)

    def __repr__(self):
        return "<Task %r>" % self.id


@app.route("/", methods=["POST", "GET"])
def index():
    if request.method == "POST":
        task_content = request.form["content"]
        new_task = Todo(content=task_content)
        try:
            db.session.add(new_task)
            db.session.commit()
            return redirect("/")
        except:
            return "There was an issue adding your task"
    else: 
        tasks = Todo.query.order_by(Todo.date_created).all()
        return render_template("index.html", tasks=tasks)
        # return "hello"


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

这是错误:

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) 没有这样的表:todo [SQL: SELECT todo.id AS todo_id, todo.content AS todo_content, todo.date_created AS todo_date_created FROM todo ORDER BY todo.date_created]

【问题讨论】:

    标签: python sqlite flask


    【解决方案1】:

    在尝试调用它之前,您必须使用表创建数据库。在错误文本中回答。

    尝试阅读documentation 有很多信息给你。检查方法:create_all 和文档中的第二个代码示例:

    >>> from yourapplication import db
    >>> db.create_all()
    

    【讨论】:

      猜你喜欢
      • 2016-12-15
      • 2017-06-13
      • 2020-05-31
      • 2021-01-24
      • 2022-07-28
      • 2018-10-05
      • 2020-12-07
      • 2022-10-18
      • 2020-12-01
      相关资源
      最近更新 更多