I am working on a database that will be populated using a Python script that reads data from a Excel file and then executes SQLite Queries. The data in the Excel file will be updated regularly and in addition to that will also contain incomplete data.
So I am trying to find a SQLite statement that will allow data to be inserted if the data is ‘correct and new’ or updated if the data is changed.
I do not want to add NULL data to the database tables, because that would defeat the purpose of moving the data from a excel file to the DB. Also I do not want to filter the data in Python because I have a cool database that should be able to handle that. (If I can figure out how to tell it to actually do it)
First I am making a Table (I am doing this once on creation of the project)
CREATE TABLE IF NOT EXISTS 'ImportantTable'
(ID INTEGER NOT NULL,
Foo TEXT NOT NULL,
PRIMARY KEY (ID))
So far so good.
Next step is to fill the table with data. Since I am using Python I wrote a function that dynamically creates a query that can work for multiple tables. Which works alright.
The Query that does work looks like
INSERT OR IGNORE INTO 'ImportantTable'
VALUES
(0, 'Bar'),
(1, 'Spam'),
(2, 'MoreSpam'),
(3, NULL)
The ID number will match with ab ID on the original Excel file, so it is alright if that skips.
The main issue I have is that with this method I cannot update the rows since it will ignore them if they are already existing. No problem I thought, I will use INSERT OR REPLACE
. But then the Query aborts due to Not Null constraint errors.
I have tried adding ON CONFLICT DO NOTHING
to the end of the query, but this gives me syntax errors.
Code:
INSERT OR REPLACE INTO 'ImportantTable'
VALUES
(0, 'Bar'),
(1, 'Spam'),
(2, 'MoreSpam'),
(3, NULL)
ON CONFLICT DO NOTHING
I have tried following the documentation Upsert-Clause but probably I am misunderstanding some things.
Some other variations of the ON CONFLICT
I have tried are:
ON CONFLICT ON CONSTRAINT DO NOTHING
ON CONFLICT IGNORE
ON CONFLICT ON CONSTRAINT NOTNULL DO NOTHING
ON CONFLICT NOTNULL DO NOTHING
So, I am at a loss here on how to achieve this.