首页 > 解决方案 > 错误:sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) 没有这样的表:

问题描述

Base.html



<!DOCTYPE html>
<html lang="en">

<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Todo App</title>

 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css">
 <script src="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.js"></script>
</head>

<body>
 <div style="margin-top: 50px;" class="ui container">
     <h1 class="ui center aligned header">To Do App</h1>

     <form class="ui form" action="/add" method="post">
         <div class="field">
             <label>Todo Title</label>
             <input type="text" name="title" placeholder="Enter Todo..."><br>
         </div>
         <button class="ui blue button" type="submit">Add</button>
     </form>

     <hr>

     {% for todo in todo_list %}
     <div class="ui segment">
         <p class="ui big header">{{todo.id }} | {{ todo.title }}</p>

         {% if todo.complete == False %}
         <span class="ui gray label">Not Complete</span>
         {% else %}
         <span class="ui green label">Completed</span>
         {% endif %}

         <a class="ui blue button" href="/update/{{ todo.id }}">Update</a>
         <a class="ui red button" href="/delete/{{ todo.id }}">Delete</a>
     </div>
     {% endfor %}
 </div>
</body>

</html>

应用程序.py


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

app = Flask(__name__)

# /// = relative path, //// = absolute path
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)


class Todo(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100))
    complete = db.Column(db.Boolean)


@app.route("/")
def home():
    todo_list = Todo.query.all()
    return render_template("base.html", todo_list=todo_list)


@app.route("/add", methods=["POST"])
def add():
    title = request.form.get("title")
    new_todo = Todo(title=title, complete=False)
    db.session.add(new_todo)
    db.session.commit()
    return redirect(url_for("home"))


@app.route("/update/<int:todo_id>")
def update(todo_id):
    todo = Todo.query.filter_by(id=todo_id).first()
    todo.complete = not todo.complete
    db.session.commit()
    return redirect(url_for("home"))


@app.route("/delete/<int:todo_id>")
def delete(todo_id):
    todo = Todo.query.filter_by(id=todo_id).first()
    db.session.delete(todo)
    db.session.commit()
    return redirect(url_for("home"))

    @app.before_first_request
    def create_tables():
        db.create_all()


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

后续步骤:

$ mkdir myproject
$ cd myproject
$ python3 -m venv venv

激活它

$ . venv/bin/activate

或在 Windows 上

venv\Scripts\activate

安装烧瓶

$ pip install Flask
$ pip install Flask-SQLAlchemy

在终端中设置环境变量

$ export FLASK_APP=app.py
$ export FLASK_ENV=development

或在 Windows 上

$ set FLASK_APP=app.py
$ set FLASK_ENV=development

运行应用程序

$ flask run

错误信息:

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: todo [SQL: SELECT todo.id AS todo_id, todo.title AS todo_title, todo.complete AS todo_complete FROM todo] (此错误的背景:https:/ /sqlalche.me/e/14/e3q8 ) 调试器在您的 WSGI 应用程序中捕获到异常。您现在可以查看导致错误的回溯。

标签: python-3.xflask-sqlalchemy

解决方案


文档中所述,OperationalError当 api 无法解析驱动程序时发生。从您的代码中,一种可能的故障排除可能是在 app.py 中重命名此行:

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite' # <--- remove this
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite3'# <--- add this
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

原因: 连接可能没有很好地建立,并且必须未能创建所需的数据库。上述建议的编辑可能会解决此问题

编辑:如果上述答案对您不起作用,请查看此答案


推荐阅读