我是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;
语法有问题吗?
答案 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 "