OperationalError:没有这样的表:关于Python书籍的任何地方

问题描述 投票:2回答:1

我有蟒蛇的文件结构的任何地方为:

flaskhost(文件夹),其包含:

  1. app.朋友
  2. books.db

app.py包含 - :

import flask
from flask import request, jsonify
import sqlite3

app = flask.Flask(__name__)
app.config["DEBUG"] = True

def dict_factory(cursor, row):
    d = {}
    for idx, col in enumerate(cursor.description):
        d[col[0]] = row[idx]
    return d


@app.route('/', methods=['GET'])
def home():
    return '''<h1>Distant Reading Archive</h1>
<p>A prototype API for distant reading of science fiction novels.</p>'''


@app.route('/api/v1/resources/books/all', methods=['GET'])
def api_all():
    conn = sqlite3.connect('books.db')
    conn.row_factory = dict_factory
    cur = conn.cursor()
    all_books = cur.execute('SELECT * FROM books;').fetchall()

    return jsonify(all_books)



@app.errorhandler(404)
def page_not_found(e):
    return "<h1>404</h1><p>The resource could not be found.</p>", 404


@app.route('/api/v1/resources/books', methods=['GET'])
def api_filter():
    query_parameters = request.args

    id = query_parameters.get('id')
    published = query_parameters.get('published')
    author = query_parameters.get('author')

    query = "SELECT * FROM books WHERE"
    to_filter = []

    if id:
        query += ' id=? AND'
        to_filter.append(id)
    if published:
        query += ' published=? AND'
        to_filter.append(published)
    if author:
        query += ' author=? AND'
        to_filter.append(author)
    if not (id or published or author):
        return page_not_found(404)

    query = query[:-4] + ';'

    conn = sqlite3.connect('books.db')
    conn.row_factory = dict_factory
    cur = conn.cursor()

    results = cur.execute(query, to_filter).fetchall()

    return jsonify(results)

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

我想按照这个教程

https://programminghistorian.org/en/lessons/creating-apis-with-python-and-flask

我的网站托管在:

http://vivanks.pythonanywhere.com

但是,当我通过调用API

http://127.0.0.1:5000/api/v1/resources/books?author=Connie+Willis

它告诉我的错误:

sqlite3.OperationalError:没有这样的表:书籍

任何帮助如何解决这一问题,并在pythonanywhere.com主机应用程序吗?

P.S在我的本地机器它的工作完全没有问题

python sqlite flask pythonanywhere
1个回答
4
投票

在Pythonanywnere,当指着含量比模板或静态文件等(存储在自己的正确的目录,由flask访问),你必须提供完整的路径:

conn = sqlite3.connect('/home/your_username/flaskhost/books.db')
© www.soinside.com 2019 - 2024. All rights reserved.