2013-04-27 148 views
3

我已經在Android應用程序創建我sqlLite DB驗證碼:android.database.sqlite.SQLiteConstraintException:外鍵約束失敗(代碼19)

private static final String CREATE_TABELLA_PERSONE = "CREATE TABLE TB_PERSONE " 
+ "(_ID integer," 
+ "CODICE_PERSONA text not null, DESC_PERSONA text null, " 
+ "PRIMARY KEY (CODICE_PERSONA));"; 

private static final String CREATE_TABELLA_USCITE = "CREATE TABLE AN_USCITE " 
+ "(_ID integer, " 
+ "CODICE_USCITA text null, SCADENZA text null, DATA text null," 
+ "PERSONA text not null REFERENCES TB_PERSONE(CODICE_PERSONA) ON DELETE NO ACTION ON UPDATE  CASCADE,PRIMARY KEY(CODICE_USCITA));"; 

當我調用函數DELETE在「TB_PERSONE」中排一行

public int SQLdelete(String tabella, String id) { 
    return database.delete(tabella, "_ID=" + id, null); 
} 

該應用程序向我顯示此錯誤:

04-27 11:15:27.152: E/AndroidRuntime(22031): FATAL EXCEPTION: main 
04-27 11:15:27.152: E/AndroidRuntime(22031): android.database.sqlite.SQLiteConstraintException:   
foreign key constraint failed (code 19) 
04-27 11:15:27.152: E/AndroidRuntime(22031): at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method) 
04-27 11:15:27.152: E/AndroidRuntime(22031): at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:727) 
04-27 11:15:27.152: E/AndroidRuntime(22031): at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754) 
04-27 11:15:27.152: E/AndroidRuntime(22031): at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64) 
04-27 11:15:27.152: E/AndroidRuntime(22031): at android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1494) 
04-27 11:15:27.152: E/AndroidRuntime(22031): at it.jackpot21.personalmoney.DbAdapter.SQLdelete(DbAdapter.java:89) 
04-27 11:15:27.152: E/AndroidRuntime(22031): at it.jackpot21.personalmoney.PersoneActivity$5.onClick(PersoneActivity.java:215) 
04-27 11:15:27.152: E/AndroidRuntime(22031): at android.view.View.performClick(View.java:4084) 
04-27 11:15:27.152: E/AndroidRuntime(22031): at android.view.View$PerformClick.run(View.java:16966) 
04-27 11:15:27.152: E/AndroidRuntime(22031): at android.os.Handler.handleCallback(Handler.java:615) 
04-27 11:15:27.152: E/AndroidRuntime(22031): at android.os.Handler.dispatchMessage(Handler.java:92) 
04-27 11:15:27.152: E/AndroidRuntime(22031): at android.os.Looper.loop(Looper.java:137) 
04-27 11:15:27.152: E/AndroidRuntime(22031): at android.app.ActivityThread.main(ActivityThread.java:4745) 
04-27 11:15:27.152: E/AndroidRuntime(22031): at java.lang.reflect.Method.invokeNative(Native Method) 
04-27 11:15:27.152: E/AndroidRuntime(22031): at java.lang.reflect.Method.invoke(Method.java:511) 
04-27 11:15:27.152: E/AndroidRuntime(22031): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
04-27 11:15:27.152: E/AndroidRuntime(22031): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
04-27 11:15:27.152: E/AndroidRuntime(22031): at dalvik.system.NativeStart.main(Native Method) 

有人可以幫我嗎?
非常感謝! :)

編輯:
我解決了它通過把ON DELETE SET NULL
謝謝!

+0

你試過了什麼?檢查此鏈接:http://stackoverflow.com/questions/8117685/android-database-sqlite-sqliteconstraintexception-error-code-19-constraint-fai – Maulik 2013-04-27 11:55:54

+0

,並檢查這一個:http://stackoverflow.com/questions/ 14929277/error-inserted-android-database-sqlite-sqliteconstraintexception-error-code-19 – Maulik 2013-04-27 12:01:41

回答

0

這意味着AN_USCITE表中仍包含一些引用您要刪除的人的記錄。

2

看起來您正在從TB_PERSONE刪除一條記錄,該記錄來自表AN_USCITE的記錄。

所以,

1)刪除該記錄從AN_USCITETB_PERSONE刪除記錄之前。

2)設置ON DELETE CASCADE在你的外鍵的定義:

... 
PERSONA text not null REFERENCES TB_PERSONE(CODICE_PERSONA) ON DELETE CASCADE ... 
... 
+0

但是我不能刪除'AN_USCITE'中的記錄,如果我在'TB_PERSONE'中刪除一個記錄。 – Hikari 2013-04-28 06:25:34

+0

如果你仍然需要AN_USCITE,你可能需要一些映射表 – Kitesurfer 2014-04-04 01:25:50

0

請確保String id沒有空,因此您在下面的代碼可以執行。

public int SQLdelete(String tabella, String id) { 
    return database.delete(tabella, "_ID=" + id, null); 
} 
相關問題