开发者

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")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜