开发者

SQlite Trigger Execution Problem

开发者 https://www.devze.com 2023-03-12 17:07 出处:网络
I\'m new to Android & SQlite. I was trying to create the following trigger on an SQLite database in Android.

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 "
0

精彩评论

暂无评论...
验证码 换一张
取 消