2016-06-27 57 views
1

我已經實現大多數其他基本數據庫事務,包括插入,更新,具有類似的語法選擇,但在試圖刪除,我得到錯誤Python的連接錯誤

mysql.connector.errors.ProgrammingError: 1064 (42000): 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 '%s' at line 1

會是什麼正確的語法是?我必須根據用戶輸入刪除。這裏是我的代碼縮短版,減去INSERT,SELECT,UPDATE部分:

elif (choice == 4): 
    mail=raw_input('Enter email of user to be deleted:') 
    print 'Deleting..' 
    delete_user_details(mail) 


def delete_user_details(email): 

    sql = "DELETE FROM users WHERE email = %s" 
    cursor.execute(sql,email) 
+0

這就是你所有的代碼? sql行不在第1行 –

+0

知道錯誤是在第一行,也許你的連接字符串有一些問題?你應該發佈更多。 – isamert

回答

3

你需要傳遞查詢參數cursor.execute()作爲元組,即使是單一的參數。試試這個:

sql = "DELETE FROM users WHERE email = %s" 
cursor.execute(sql, (email,)) 
+0

啊!非常感謝!!我不知道我在文檔中錯過了什麼。 – momo

+2

愚蠢的語法錯誤就像在元組中缺少逗號讓我頭痛。總是很高興看到正確的代碼,並注意到我的錯誤!謝謝 – GHandel