I need to make a statement that adds an user to my SQLite database if it doesn't exist, otherwise update its values and add 1 to his previous totalMessages.
So far I wrote this:
INSERT OR REPLACE INTO users (id, username, first, last, totalMessages) values (
'hisID',
'hisName',
'hisFirst',
'hisLast',
(SELECT totalMessages FROM users WHERE id = 'hisID') +1);
It works fine when the user already exists, but when it doesn't, the value of totalMessages is set to NULL, and I need it to be 0.
This is my users table:
CREATE TABLE IF NOT EXISTS users (
id VARCHAR(255) PRIMARY KEY,
username VARCHAR(255),
first VARCHAR(255),
last VARCHAR(255),
totalMessages INTEGER)
Can anybody tell me how to do it, please?
UPDATE: I tried using "DEFAULT '0'" and "DEFAULT 0" as Scott suggested, but it is still saving totalMessages as NULL.
You can see it and test is here.