如何通過django更新mysql數據?我也沒有收到任何錯誤。如何使用django更新mysql數據?
views.py
def update_db_data(request):
conn = MySQLdb.connect(host= "localhost", user="test_user", passwd="test_pwd",db="myproject")
cursor = conn.cursor()
try:
cursor.execute("UPDATE user SET user_name = 'test'")
print("sucess")
html = "<html><body>sucess</body></html>"
conn.commit()
except:
print("fail")
html = "<html><body>fail</body></html>"
conn.rollback()
conn.close()
return HttpResponse(html)
請告訴我哪裏是我的代碼的問題。 如何做條件更新?
eg:- UPDATE user SET user_name = 'test' where id =2;
您可以使用Django-ORM爲這個簡單的查詢。爲什麼不使用它? –