如何通过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为这个简单的查询。为什么不使用它? –