I'm new to Android & SQlite. I was trying to create the following trigger on an SQLite database in Android.
final String CREATE_TRIGGER_STATES =
"CREATE TRIGGER fk_insert_state BEFORE "
+ "INSERT on tbl_states"
+ "FOR EACH ROW "
+ "BEGIN "
+ "SELECT RAISE(ROLLBACK, 'insert on table "
+ "\"tbl_states\" violates foreign key constraint "
+ "\"fk_insert_state\"') WHERE (SELECT id FROM "
+ "tbl_countries WHERE id = NEW.country_id) IS NULL; "
+ "END;";
db.execSQL(CREATE_TRIGGER_STATES);
Error log:
开发者_高级运维android.database.sqlite.SQLiteException: near "EACH": syntax error: CREATE TRIGGER fk_insert_state BEFORE INSERT on tbl_statesFOR EACH ROW BEGIN SELECT RAISE(ROLLBACK, 'insert on table "tbl_states" violates foreign key constraint "fk_insert_state"') WHERE (SELECT id FROM tbl_countries WHERE id = NEW.country_id) IS NULL; END;
Is there a problem with the syntax ?
I'm guessing that the relevant portion of the error message is right here:
... on tbl_statesFOR EACH ROW ...
//--------------^^
You're missing a space after tbl_states
:
final String CREATE_TRIGGER_STATES =
"CREATE TRIGGER fk_insert_state BEFORE "
+ "INSERT on tbl_states " // Missing space added
//... Continue on as before.
You forgot a space:
+ "INSERT on tbl_states " // Space missing here
+ "FOR EACH ROW "
精彩评论