2011-06-13 79 views
1

我是Android新手& SQlite。我試圖在Android中的SQLite數據庫上創建以下觸發器。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

我猜,錯誤信息的相關部分就在這裏:

... 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. 
+0

是的,這是問題,它現在的作品!謝謝 – 2011-06-13 06:32:32

1

你忘了空間:

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