1.4.4.5 Deleting Data

For example consider the table we created earlier:

# table Person
+----------+-----------+------------------+-------------+
| LastName | FirstName | Address          | DateOfBirth |
+----------+-----------+------------------+-------------+
| Smith    | John      | 5 Friendly Place | 1980-01-01  |
| Owen     | Jones     | 4 Great Corner   | 1990-01-01  |
+----------+-----------+------------------+-------------+

The SQL command to delete every address in the table is:

DELETE FROM Person

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

cursor.delete("Person")

Note: This does not delete the table, it deletes all the data. To drop the table use the drop() method.

The table now looks like this:

+----------+-----------+---------+-------------+
| LastName | FirstName | Address | DateOfBirth |
+----------+-----------+---------+-------------+
+----------+-----------+---------+-------------+

To delete only some of the data you need to specify the where parameter. For example to delete all people with the first name 'Owen' we would use the SQL:

DELETE FROM Person WHERE FirstName='Owen'

Similarly the function to use to execute this SQL command is:

cursor.delete("Person", where="FirstName='Owen'")

The table now looks like this:

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

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

delete( table, [where][, autoExecute])
table should be the table name as a string. 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.