1.4.4.4 Updating Data

For example consider the table we created earlier:

# table Person
+----------+-----------+------------------+-------------+
| LastName | FirstName | Address          | DateOfBirth |
+----------+-----------+------------------+-------------+
| Smith    | John      | 5 Friendly Place | 1980-01-01  |
+----------+-----------+------------------+-------------+

The SQL command to change every address in the table to '6 London Road' is:

UPDATE Person SET Address = '6 London Road'

To update the data using a web.database cursor we would do the following:

cursor.update('Person','Address','6 London Road')

The table now looks like this:

+----------+-----------+---------------+-------------+
| LastName | FirstName | Address       | DateOfBirth |
+----------+-----------+---------------+-------------+
| Smith    | John      | 6 London Road | 1980-01-01  |
+----------+-----------+---------------+-------------+

The update() method of a web.database cursor looks like this:

update( table, fields, values [, where][, autoExecute])
table should be the table name as a string. fields is a list of the field names to insert values for, it can be a string if there is only field. values is a list of the values corresponding to the fields in fields, again it can be a string if there is only field. where is an SQL where clause. autoExecute is optional and can be True or False and by default takes the value of the cursor object.

See About this document... for information on suggesting changes.