【发布时间】:2016-12-26 05:58:03
【问题描述】:
如何通过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。为什么不使用呢?