1

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 ?

2 Answers 2

2

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.
Sign up to request clarification or add additional context in comments.

Comments

1

You forgot a space:

 + "INSERT on tbl_states "    // Space missing here
 + "FOR EACH ROW "

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.