![](/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.