Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine if a Python sqlite UPDATE worked?

Tags:

I'm using sqlite3 in Python. I want to know if my UPDATE statement worked or not without doing another database query:

c.execute('update students set gpa=3.5 where stuid=123')

If there isn't a student with stuid 123 then obviously the update fails.

like image 966
tronman Avatar asked Feb 15 '11 17:02

tronman


People also ask

Does SQLite update?

Introduction to SQLite UPDATE statement First, specify the table where you want to update after the UPDATE clause. Second, set new value for each column of the table in the SET clause. Third, specify rows to update using a condition in the WHERE clause. The WHERE clause is optional.


2 Answers

cursor.rowcount will be 1 if the update was successful (affecting 1 row) or 0 if it failed.

like image 103
Mark Rushakoff Avatar answered Oct 08 '22 10:10

Mark Rushakoff


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
like image 20
Kyle Krzeski Avatar answered Oct 08 '22 12:10

Kyle Krzeski