Fixed several problems in the DB triggers.
Made the database direct queries SQLite compliant.