myapp.py
#!flask/bin/python
from flask import Flask, request, jsonify
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
db = SQLAlchemy(app)
app.config['SQLAlchemy_DATABASE_URI'] = 'mysql://root:root@localhost/linkmanager'
class Link(db.Model):
__tablename__ = 'link'
id = db.Column(db.Integer, primary_key = True)
title = db.Column(db.Text)
url = db.Column(db.Text)
type = db.Column(db.Integer)
insert_date = db.Column(db.DateTime)
@app.route('/links', methods=['GET'])
def links():
if request.method == 'GET':
results = Link.query.limit(10).offset(0).all()
json_results = []
for result in results:
d = {
'id': results.id,
'title': results.title,
'url': results.url,
'type': results.type,
'insert_date': results.insert_date
}
json_results.append(d)
return jsonify(items=json_results)
if __name__ == '__main__':
app.run(debug=True)
当我尝试访问localhost:5000 / links:
时出现错误消息sqlalchemy.exc.OperationalError OperationalError:(OperationalError)没有这样的表:link u'SELECT link.id AS link_id,link.title AS link_title,link.url AS link_url,link.type AS link_type,link.insert_date AS link_insert_date \ nFROM link \ n LIMIT? OFFSET?' (10,0)
我知道该表确实存在,因为我使用phpAdmin进行设置并从命令行访问它。我也尝试用Java连接它并且成功了...我不知道为什么这不起作用!谢谢你的帮助。
答案 0 :(得分:2)
您的设置名称错误。变化
app.config['SQLAlchemy_DATABASE_URI'] = 'mysql://root:root@localhost/linkmanager'
到
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:root@localhost/linkmanager'