我測試Android中的外鍵,我有問題,我不明白:的Android SQLite和外鍵遇事
創建表(通過硬編碼值)
db.execSQL("CREATE TABLE IF NOT EXISTS table_A (" +
"_id long primary key , value1 long );");
db.execSQL("CREATE TABLE IF NOT EXISTS table_B (" +
"_id long primary key , value1fk long , value2 long,"+
"FOREIGN KEY (value1fk) REFERENCES table_A (value1) ON DELETE CASCADE);");
然後我執行:
ContentValues values = new ContentValues();
values.put("_id", 1);
values.put("value1", 200);
long result = mDb.insert("table_A", null, values);
Log.e("","done (" + result + ")");
values = new ContentValues();
values.put("_id", 1);
values.put("value1fk", 200);
values.put("value2", 10);
result= mDb.insert("table_B", null, values);
Log.e("","done (" + result + ")");
輸出是
done(1)
done(-1)
給予二次插入的外鍵不匹配
E/SQLiteDatabase(25476): Error inserting _id=1 value1fk=200 value2=10
E/SQLiteDatabase(25476): android.database.sqlite.SQLiteException: foreign key mismatch: , while compiling: INSERT INTO table_B(_id,value1fk,value2) VALUES (?,?,?)
這是爲什麼發生?
這是在副本中的錯誤與後來的代碼(編輯) – Addev 2012-04-13 15:53:55