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(table="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(table="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:
table, [values=[]][, where] [, execute]) |
?
in the WHERE
clause specified by where.
cursor.where()
. If where is a string it is converted to the correct format.
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