3

我有这段代码用于在 android 应用程序中创建我的 sqlLite DB:

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

当我在“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.
非常感谢!

4

3 回答 3

2

您似乎正在从 中删除一条记录TB_PERSONE,该记录由表中的记录引用AN_USCITE

所以,

1) 在从AN_USCITE中删除记录之前删除记录TB_PERSONE

或者

2)ON DELETE CASCADE在你的外键定义中设置:

...
PERSONA text not null REFERENCES TB_PERSONE(CODICE_PERSONA) ON DELETE CASCADE ...
...
于 2013-04-27T12:06:25.320 回答
0

请确保String id不为空,以便可以执行下面的代码。

public int SQLdelete(String tabella, String id) {
    return database.delete(tabella, "_ID=" + id, null);
}
于 2014-07-01T07:20:55.510 回答
0

这意味着该AN_USCITE表仍然包含一些引用您要删除的人的记录。

于 2013-04-27T12:02:53.427 回答