SQlite触发器执行问题

时间:2011-06-13 06:12:05

标签: android sqlite triggers

我是Android和Android的新手SQlite的。我试图在Android中的SQLite数据库上创建以下触发器。

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);

错误日志:

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;

语法有问题吗?

2 个答案:

答案 0 :(得分:2)

我猜错误信息的相关部分就在这里:

... on tbl_statesFOR EACH ROW ...
//--------------^^

tbl_states之后你错过了一个空格:

final String CREATE_TRIGGER_STATES = 
            "CREATE TRIGGER fk_insert_state BEFORE "
            + "INSERT on tbl_states " // Missing space added
            //... Continue on as before.

答案 1 :(得分:1)

你忘记了一个空间:

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