如何使用python flask

时间:2018-02-21 10:58:06

标签: python flask flask-sqlalchemy flask-restful

我正在尝试使用下面的代码,但会出错。

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_restless import APIManager

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///compData.db'
db = SQLAlchemy(app)
db.Model.metadata.reflect(db.engine)

class getRecord(db.Model):
    __table__ = db.Model.metadata.tables['condition']

@app.route('/sample', methods=["GET"])
def home():
    b = getRecord.query.all()
    for usr in b:
        return usr.first_name

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

TraceBack:

    Traceback (most recent call last):
  File "path\JetBrains\PyCharm Community Edition 2016.3\helpers\pydev\pydevd.py", line 1596, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "path\JetBrains\PyCharm Community Edition 2016.3\helpers\pydev\pydevd.py", line 974, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "path/PycharmProjects/testGetConn/test.py", line 17, in <module>
    class conda(db.Model):
  File "path/PycharmProjects/testGetConn/test.py", line 18, in conda
    __table__ = db.Model.metadata.tables['condition']
KeyError: 'condition'

通过使用这个tutorial我在该表上使用SQLAlchemy创建了表,上面的代码工作正常,但是当我试图从另一个DB访问它时显示错误。

0 个答案:

没有答案