2

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.

1 Answer 1

7

Try

INSERT OR REPLACE INTO SearchTable(Owner, Generations) SELECT ?, MAX(Generations) FROM SearchTable WHERE Owner = ? 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks peterm. I ended up using: INSERT OR REPLACE INTO SearchTable(Owner, Generations) SELECT ?, MIN(?, COALESCE(Generations, 999)) FROM SearchTable WHERE Owner = ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.