2017-04-22 62 views
0

我的數據庫中有一條未執行的更新語句。據我所知,它在語法上是正確的。我用this app來驗證語法。 except塊是所有正在執行,我不明白爲什麼。未能在Python CGI中執行的SQL語句

下面是代碼:

for res_posts in list_of_response_ids: 
temp_str = res_posts[0] # first element of res_posts tuple is string 
temp_str += ":" + str(output[0]) 
try: 
    sql = "UPDATE POST SET res_post_id = %s WHERE post_id = %d;" % (str(temp_str), int(res_posts[1])) 
    cursor.execute(sql) 
except: 
    print "uh oh" 

我可以發佈更多的代碼,如果這是沒有足夠的信息。

編輯:繼雅各的建議,我用raise並得到了以下錯誤:

Traceback (most recent call last): 
    File "post.cgi", line 93, in <module> 
    cursor.execute(sql) 
    File "/usr/lib64/python2.6/site-packages/MySQLdb/cursors.py", line 173, in  execute 
self.errorhandler(self, exc, value) 
    File "/usr/lib64/python2.6/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler 
    raise errorclass, errorvalue 
ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':37 WHERE post_id = 8' at line 1") 

太謝謝你了!

+0

在打印普通的消息,除了塊需要扔掉由蟒蛇提供的異常詳細信息。我建議在打印語句後在行上添加'raise',以便可以看到完整的堆棧跟蹤。如果您需要幫助解釋此跟蹤的含義,請將其粘貼到此處。 – JacobIRR

+0

我已將輸出添加到我的帖子中。任何想法有什麼問題的語法? – Azhraam

回答

1

根據您的回溯,您正在使用res_post_idpost_id的條目類型有問題。目前您未傳遞res_post_id字符串的表示法,而是字符串。如果res_post_id是在你的DB模式的字符串,我會建議使用%R,像這樣:

sql = "UPDATE POST SET res_post_id = %r WHERE post_id = %d;" % (str(temp_str), int(res_posts[1])) 

這將正確引用您res_post_id值插入。

所以,你的說法應該從:

UPDATE POST SET res_post_id = :37 WHERE post_id = 8; 

...這樣的:

UPDATE POST SET res_post_id = ':37' WHERE post_id = 8; 
+0

哇!沒有意識到它必須如此具體!修好了,非常感謝你! – Azhraam