1.4.13.3 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(table='Person',columns=['Address'],values=['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, columns, values, __sqlValues [, where] [, execute])
Update the columns in table with the values. Either values or _sqlValues can be specified but not both.
table
A string containing the name of the table to update
columns
A sequence of column names in the same order as the values which are going to be updated in those columns. Can be a string if only one column is going to have values inserted
values
A sequence of Python values to be inserted into the columns named in the columns variable. Can be the value rather than a list if there is only one value. If values is specified then _sqlValues must be either an empty sequence or contain a list of all quoted SQL strings for the columns specified in which case values contains the Python values of the SQL strings to be substituted for ? parameters in the _sqlValues sequence. If there are more values specified in values than _sqlValues the remaining values are used to substitute for ? parameters in where.
_sqlValues
A sequence of quoted SQL strings to be inserted into the columns named in the columns variable. Can be the value rather than a list if there is only one value. If _sqlValues is specified and contains ? parameters for substitution then values contains the values to be substituted. Otherwise values must be an empty sequence.
where
The WHERE clause as a web.database list as returned by cursor.where(). If where is a string it is converted to the correct format.
execute
If False the method returns the SQL string to perform the desired operations. If True the SQL is executed. If not specified takes the value specified in the cursor which by default is True