sqlite - Insert or replace doesn't work -
i need insert or replace existing db, don't work correctly.
example:
select * tab name = 'foo'; 6530|foo|ok||| insert or replace tab values('foo', 'ok', 1, 2, 3); select * tab name = 'foo'; 6530|foo|ok||| 43523|foo|ok|1|2|3
why insert-replace doesn't "update" existing fields record 6530 , creates new one?
the table:
create table tab( id integer unique primary key, name text not null, tipo text not null, val1 integer, val2 integer, val3 integer );
you need have unique index on 1 or combination of columns like:
create unique index idx_tab on tab (name, tipo);
the records gets deleted , inserted new values when same values exists in table. can upsert following:
insert or replace tab (id, name, tipo, val1, val2, val3) select old.id, new.name, new.tipo, new.val1, new.val2, val3 ( select 'foo' name, 'ok' tipo, 1 val1, 2 val2, 3 val3 ) new left join ( select id, name, tipo, val1, val2, val3 tab ) old on new.name= old.name;
Comments
Post a Comment