python-3.x - 错误: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 应用程序中捕获到异常。您现在可以查看导致错误的回溯。
解决方案
如文档中所述,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
原因: 连接可能没有很好地建立,并且必须未能创建所需的数据库。上述建议的编辑可能会解决此问题
编辑:如果上述答案对您不起作用,请查看此答案。
推荐阅读
- elixir - Phoenix:是否可以从控制器更新存在?
- sas - SAS:计算中位数、最小值和最大值时如何包含零或缺失值?
- javascript - 登录后保持会话 - selenium - javascript
- twilio - 转换 TWIML(Gather and Dial)以使用 API 和 VOIP
- ios - 用户从 iPhone/iPad(任何浏览器)专门访问时无法加载网站,甚至无法留下服务器日志
- windows - Windows OpenFile 标志
- rubygems - 一个人如何一起开发一对宝石,一个使用另一个?
- angular - Angular 2 / RxJS - http返回400时订阅中断
- python - 在 django ORM 中使用过滤器获取数据
- frama-c - 使用 CVC4 证明器时,frama-c wp 插件语法错误