Triggers

In any database, when data is inserted, updated, or deleted, you can have the table launch a trigger. For example, if a user inserts a record, you could launch a trigger to make sure that the record meets some specified criteria—no null values. Spatial databases allow you to use the same triggers. You can create these in several languages, including Python and SQL. The following example will use PL/pgsql.

You create triggers using SQL expressions. The following code will create a trigger to prevent entering an incomplete incident:

query=('CREATE FUNCTION newcrime()'+'\n' 'RETURNS trigger' +'\n' 'AS $newcrime$' +'\n' 'BEGIN' +'\n' 'IF NEW.crimetype IS NULL THEN'+'\n' 'RAISE EXCEPTION' +" '% Must Include Crime Type', NEW.address;"+'\n' ...

Get Mastering Geospatial Analysis with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.