0

I'm working on an Android app and from time to time I need to update the SQLite database with some data coming from the server.

It might happen that the data that I'm trying to insert generates conflicts, because a row with the same primary key is already present. This is why I am using insertWithOnConflict with the CONFLICT_IGNORE flag. The idea is "if the data is not present store it, if it is already present just ignore and move on".

However when a conflict happens I see that my app crashes with this exception:

E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.novadart.booster.android, PID: 1643
    android.database.sqlite.SQLiteConstraintException: foreign key constraint failed (code 19)
            at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
            at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
            at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
            at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
            at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)

why an exception when documentation says that it should not be thrown? Is is a bug or am I missing something?

If, for example, I use insert I can see the same exceptions in the logs but the app does not crash (the method returns -1 and that's it).

Giordano
  • 1,401
  • 15
  • 26
  • http://stackoverflow.com/questions/15443913/sqlite3-foreign-key-constraint-failed use this link. – Piyush Nov 10 '14 at 13:22

1 Answers1

1

the problem was that the row I was inserting was violating a foreign key constraint (the referenced row did not exist). For some reason I overlooked that.

So, it is not insertOnConflict's fault.

Giordano
  • 1,401
  • 15
  • 26
  • @piyush thanks for your answer, pointed me to the right direction even though it was not addressing the problem (I was not removing the row). – Giordano Nov 10 '14 at 13:22