app explosion with conflicting sql primary key
I have a table in a database where the _id column is the primary key. If I try to do an insert into开发者_运维问答 this table using an _id that already exists, my application crashes. How can I do this? Is there a specific kind of Exception I can eat? I've tried SQLiteException to no avail.
By definition the primary key needs to be unique -- a single value must reference a single row.
You can only do what you want by changing something -- either the database structure, so this column is no longer defined as a primary key (though think carefully before you do this -- any application written against the old schema will assume, when selecting based on the _id field, that a single id will yield a single row, and you will break that assumption, and very likely the application). The other thing is to change the data to give it a new _id.
I've figured out my problem.
I'm now using insertOrThrow and catching SQLiteExceptions. Also, I was throwing another exception all the way to the top without catching it anywhere. I am now catching and logging this.
精彩评论