3

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

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"

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!

2

3 Answers 3

2

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

So,

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

or

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

...
PERSONA text not null REFERENCES TB_PERSONE(CODICE_PERSONA) ON DELETE CASCADE ...
...
Sign up to request clarification or add additional context in comments.

2 Comments

but I must not delete the record in AN_USCITE if I delete one in TB_PERSONE.
if you still need the AN_USCITE you may need some mapping table
0

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

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.