Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get information about the column caused foreign key constraint in android sqlite?

I want to know if it is possible to get information about SQLiteConstraintException: foreign key constraint exception.

I need to know which column caused a violation of foreign key constraint.

Is there any way to get these information from the exception?

Also it can be good if I could get the name of that Constraint.

like image 514
Bobs Avatar asked Jun 19 '16 05:06

Bobs


People also ask

How do you identify a foreign key constraint?

Using SQL Server Management Studio Open the Table Designer for the table containing the foreign key you want to view, right-click in the Table Designer, and choose Relationships from the shortcut menu. In the Foreign Key Relationships dialog box, select the relationship with properties you want to view.

Does SQLite enforce foreign key constraints?

SQLite has supported foreign key constraint since version 3.6. 19. The SQLite library must also be compiled with neither SQLITE_OMIT_FOREIGN_KEY nor SQLITE_OMIT_TRIGGER.

How do I fix foreign key constraint failure?

The error message itself showing there is a foreign key constraint error, which means you are deleting a parent table where the child table contains the Primary table identifier as a foreign key. To avoid this error, you need to delete child table records first and after that the parent table record.


2 Answers

I think you can find the answer in the following qestion:

Possible to get specific error details from Android SQLiteConstraintException?

Look at solution 2 posted by antoino.

like image 59
Avi K. Avatar answered Sep 21 '22 15:09

Avi K.


No direct way to find column name that violate the foreign key constraint from SQLiteConstraintException: foreign key constraint exception.

But you can find operation(delete, insert, create etc) name that raise SQLiteConstraintException: foreign key constraint exception.In following log

 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)

In the above log

04-27 11:15:27.152: E/AndroidRuntime(22031): at android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1494)

this tells us delete operation raise exception. Then you can find which line in your code raise this exception and check your query and column manually.

like image 24
USKMobility Avatar answered Sep 22 '22 15:09

USKMobility