如果不是您的方法,我建议您在提交和判断之后立即判断受影响的行.
from django.db import connection
# Initialize affected_rows outside the context manager
affected_rows = None
# Assuming you have a SQL update query
update_query = "UPDATE your_table SET your_column = your_value WHERE your_condition"
# Get a cursor
with connection.cursor() as cursor:
# Execute the update query
cursor.execute(update_query)
# Commit the transaction
cursor.connection.commit()
# Get the number of affected rows
affected_rows = cursor.rowcount
# Check if the update was successful
if affected_rows is not None and affected_rows > 0:
# Update was successful, take appropriate action
pass
else:
# No rows were affected, handle the error or raise an Exception
raise Exception("No rows were affected by the update query.")
按顺序try 此操作并查找受影响的行
- 使用
cursor.execute()
方法执行SQL查询.
- 使用
cursor.connection.commit()
方法提交事务.
- 使用
cursor.rowcount
属性可获取受影响的数量
排好了.