How do I determine if a Python sqlite UPDATE worked?
I'm using sqlite3 in Python. I want to know if my UPDATE statement worked or not without doing another database query:
c.exec开发者_如何学JAVAute('update students set gpa=3.5 where stuid=123')
If there isn't a student with stuid 123 then obviously the update fails.
cursor.rowcount
will be 1 if the update was successful (affecting 1 row) or 0 if it failed.
For a slightly more complete answer for if you want to handle error and success:
c.execute('update students set gpa=3.5 where stuid=123')
if c.rowcount < 1:
#error
else:
#success
command = """Update Table_Name SET col1 = 2 WHERE col2 = 1"""
dbconn.execute(command)
sqliteconn.commit()
if dbconn.rowcount > 0:
print("Update Statement Executed Successfully")
精彩评论