android.database.sqlite.SQLiteConstraintException:外键约束失败(代码19)

时间:2021-07-05 05:28:30

I've this code for creating my sqlLite DB in android application:

我有这个代码在android应用程序中创建我的sqlLite数据库:

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

and

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

When i call the function for DELETE a row in "TB_PERSONE"

当我在DELETE中调用函数“TB_PERSONE”中的一行时

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

the application show me this error:

该应用程序向我显示此错误:

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)

Can someone help me pls?
Thanks a lot! :)

有人可以帮我吗?非常感谢! :)

EDIT:
I solved it by putting ON DELETE SET NULL.
Thanks at all!

编辑:我通过将ON DELETE SET NULL解决了它。谢谢!

3 个解决方案

#1


2  

It seems you are deleting a record from TB_PERSONE, which is referenced by record(s) from table AN_USCITE.

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

So,

1) either delete the records from AN_USCITE prior to deleting record from TB_PERSONE.

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

or

2) set ON DELETE CASCADE in your foreign key definition:

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

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

#2


0  

This means that the AN_USCITE table still contains some record that references the person you're trying to delete.

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

#3


0  

Please make sure that String id not null so your code below can be executed.

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

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

#1


2  

It seems you are deleting a record from TB_PERSONE, which is referenced by record(s) from table AN_USCITE.

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

So,

1) either delete the records from AN_USCITE prior to deleting record from TB_PERSONE.

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

or

2) set ON DELETE CASCADE in your foreign key definition:

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

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

#2


0  

This means that the AN_USCITE table still contains some record that references the person you're trying to delete.

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

#3


0  

Please make sure that String id not null so your code below can be executed.

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

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