DELETE

Manual DELETE

We'll delete a Country in our example database. First inline:

Query.create("DELETE FROM country WHERE id = 1").delete();

Again, to avoid SQL injection we'll send in the id as a parameter:

Query.create("DELETE FROM country WHERE id = :id").param("id", 1).delete();

Delete using an existing domain object:

Query.create("DELETE FROM country WHERE id = :id")
    .param(country).delete();

We can also supply the country as a named parameter:

Query.create("DELETE FROM country WHERE id = :country.id")
    .param("country", country).delete();

DELETE with a Mapper

As with INSERT and UPDATE, Tornado Query can automatically write the DELETE statement for us:

Query.delete(countryMapper, country);

Since the countryMapper was created with an id method, it knows how to construct the WHERE clause for the delete. This is true even if there are multiple id fields in the Mapper.

NOTE: The Query.delete() method will return the number of rows that was deleted.