Insert or update sqlite3
I'm facing a small problem here. I have a table with multiple columns but
what i want to is write a sqlite query that to be used in my python script
so that i can insert if the key is not found or else update other values
if the key is already found. I researched a little and came through this
answer but my concern in my content it's not working and keeps on adding
the new rows even if it exists.
Using this i wrote a query since my update/insert is dependent on a column
called as id which is a foreign key constraint to this table. Other two
columns that i want to update are say name and role. so what i want to do
is insert all three if the id is not found. if found then see if it has
got same values or not for other two columns and update it. here is my
attempt.
INSERT OR REPLACE INTO tblExample(id, name, role) VALUES (
COALESCE((SELECT id FROM tblExample WHERE id = 1),'1'),'name','role');
But it's still inserting new rows. why? how can i avoid it? any simpler
solution?
No comments:
Post a Comment