【发布时间】:2017-12-22 12:04:46
【问题描述】:
我正在尝试为 RESTful Web 服务编写微服务。
我在“Postgresql”中创建了一个数据库,目前使用 Flask 和 psycopg2(用于将 db-object 转换为 json 对象)。
以下是我的代码的一部分,但由于某种原因,我遇到了错误。我正在尝试建立的 URI 有点像 localhost/events/20171222 处理这个问题的正确方法是什么?
代码:
app = Flask(__name__)
conn = psycopg2.connect("dbname='postgresdb'")
cur = conn.cursor(cursor_factory=RealDictCursor)
@app.route('/events/<dated>', methods=['GET'])
def getDatedEvents(dated):
date_obj = datetime.strptime(dated, '%Y%m%d')
#print(type(date_obj))
#print(date_obj)
cur.execute("""
SELECT event_id, timestamp
FROM event_tbl
WHERE timestamp < date_obj
ORDER BY timestamp
LIMIT 25
""")
return json.dumps(cur.fetchall(), default=json_serial)
错误输出:
psycopg2.ProgrammingError: column "date_obj" does not exist
LINE 4: WHERE timestamp < date_obj
^
localhost - - [22/Dec/2017 17:22:29] "GET /events/20161020 HTTP/1.1" 500 -
【问题讨论】:
标签: python postgresql rest flask