0

In my Android application I am using compiled statements to be able to insert rows quickly:

insert = db.compileStatement(
                    "INSERT INTO foo (id, aaa, bbb, ccc) " +
                    "VALUES (?,?,?,?)");

This works correctly and quite fast. However when there is already foo with id, I am receiving following exception:

column id is not unique (code 19)

What would be a correct way for overwriting existing row?

1
  • If you want to overwrite it, use the UPDATE-statement. Commented Jun 22, 2015 at 15:08

1 Answer 1

5

What would be a correct way for overwriting existing row?

Specify a conflict resolution strategy, such as

INSERT OR REPLACE INTO foo ...

If the insert would result in a conflict, the conflicting row(s) are first deleted and then the new row is inserted.

Sign up to request clarification or add additional context in comments.

1 Comment

This would work, but just be careful with it! Make absolutely sure it's what you need for your requirements. Remember, this "deletes" the row first if it exists then replaces it with the new row. This is NOT the same as a row update. If you're needing to actually update the row but not remove it, then you can first do a simple query against the table to determine if it exists, then perform an update if it does, otherwise do an insert.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.