views:

14

answers:

2

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.

A: 

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.

jsegal
Thanks for your response. What I am doing here is syncing data being pulled from a web service. Currently, the syncing is written such that a new row will never be pulled twice. However, should something slip through the cracks, I don't want the application to crash on the user. I understand what a primary key is and I would like a clean way to handle this exception.
Andrew
A: 

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.

Andrew