![](/img/trans.png)
[英]cursor.execute() returning empty list when used with parametrized query(python,sqlite)
[英]python cursor.execute returning empty
我的python代码有问题,我想用于REST API服务器。
当前的问题是当我知道值存在时,我的数据库查询返回null
特定路径的代码:
@app.route('/data/active_predicted/<int:ticketId>', methods=['GET'])
def search_db_tickId_act(ticketId):
cursor = db.cursor()
db_query = cursor.execute("select * from active_predicted where ticketId=" + str(ticketId))
json_output = json.dumps(dict(cursor.fetchall()))
cursor.close()
if not cursor.fetchall():
return "Nothing found \n SQL Query: " + "select * from active_predicted where ticketId=" + str(ticketId)
else:
return str(cursor.fetchall())
当我访问此URL时,我返回以下内容:
没有找到SQL Query:select * from active_predicted,其中ticketId = 1324
当我插入这个SQL查询时,我得到了我想要的结果,1行有2列,但似乎程序无法找到该行?
问题:
dictionary=True
; json_output
; fetchone
而不是fetchall
; cursor.close()
之后,无论你之前取得了什么,都无法从该游标中获取任何内容; 这是固定代码:
@app.route('/data/active_predicted/<int:ticketId>', methods=['GET'])
def search_db_tickId_act(ticketId):
try:
cursor = db.cursor(dictionary=True)
db_query = cursor.execute("select * from active_predicted where ticketId=%s LIMIT 1", ticketId)
row = cursor.fetchone()
if row:
return json.dumps(row)
else:
return "Nothing found \n SQL Query: " + "select * from active_predicted where ticketId=" + str(ticketId)
finally:
cursor.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.