HalR
HalR

Reputation: 11073

SQLite conditional insert or replace

I'm trying to insert or update a record into a sqlite database, and only update the value if the new value is greater than the old.

The schema is:

 CREATE table IF NOT EXISTS SearchTable
   (Owner INTEGER PRIMARY KEY, Generations INTEGER DEFAULT 0)

I've tried commands like this:

INSERT OR REPLACE INTO SearchTable(Owner, Generations)
 VALUES (?, MAX((SELECT Generations FROM SearchTable WHERE Owner = ?), ?))

but it gives me a null value for the Generations.

I could use some help.

Upvotes: 2

Views: 1365

Answers (1)

peterm
peterm

Reputation: 92785

Try

INSERT OR REPLACE INTO SearchTable(Owner, Generations)
SELECT ?, MAX(Generations) FROM SearchTable WHERE Owner = ?

Upvotes: 7

Related Questions