Updating the record for a Task
is all about constructing the correct update
query. update
will alter any records that match the conditions in the where
clause. It will change only those columns mentioned in its set
clause so we start by constructing this set
clause (highlighted).
Joining a list of parameters and interpolating it into an SQL query might be a bit overdone but if we later want to add an extra attribute, this would be very simple (and our SQL query string now fits on a single line, making it a lot easier to read and typeset).
Once we have executed the insert, we check the number of rows affected. This value is available as the rowcount
attribute of the cursor
object and should be 1
as we used the unique task_id
to select the records. If it isn't 1
, something strange has happened and we roll back the insert and raise an exception. If it went well, we commit our changes.
Chapter4/tasklistdb.py
def update(self,user): params...