11

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.

Bob
  • 22,810
  • 38
  • 143
  • 225
  • Is it possible to infer the missing constraint in your catch clause ? A data dump might help on catch. – navid Jul 02 '16 at 12:30

4 Answers4

4

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.

Community
  • 1
  • 1
Avi K.
  • 1,734
  • 2
  • 18
  • 28
  • There is no valuable data in his solution – Bob Jun 26 '16 at 08:12
  • 1
    The solution provides everything you need to get more information about the exact error. You can print the Stack Trace dump, which should be enough. – sparkonhdfs Jul 01 '16 at 20:50
1

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.

USKMobility
  • 5,721
  • 2
  • 27
  • 34
1

Newer SQLite versions add the table names to the error message, but in neither case do you get the column names.

CL.
  • 173,858
  • 17
  • 217
  • 259
0

You could copy your sql to sqlite browser, http://sqlitebrowser.org/ and attempt to replicate the error there and it should give you more information.