0

I have table like this:

CREATE TABLE "Date_Table" 
("Year" DATETIME , 
 "Month1" INTEGER, 
 "Month2" INTEGER,
 "ID" INTEGER)

Then i created inique index of 2 columns

CREATE UNIQUE INDEX field1_field2_idx ON table_name(Year, ID)

When i done i want to insert if there is none of this kind record and replace if there are.

INSERT OR REPLACE INTO Date_Table(Year, Month1, Month2, ID) Values(2012, 1, ?,1)

Everything is as it should be at this point, but if i do this insert:

INSERT OR REPLACE INTO Date_Table(Year, Month1, Month2, ID) Values(2012, ?, 1,1) 

It replace all record deleting Month1 to NULL, but i want just update in that row Month2 column.

Maybe someone could help me on this. It's about SQLite

Streetboy
  • 4,351
  • 12
  • 56
  • 101

1 Answers1

0

This question is duplicate: SQLite - UPSERT *not* INSERT or REPLACE

For your schema try something like:

INSERT OR REPLACE INTO Date_Table (Year, Month1,  Month2, ID) 
Values(
   2012, 
   (select Month1 from Data_Table where Year = 2012 and ID = 1),
   1,1) 

EDITED due OP comment:

Instead of values you can specify a select on insert.

INSERT OR REPLACE INTO Date_Table (Year, Month1, Month2, ..., Month12, ID) 
select
   2012 as Year, 
   Month1,
   Month2,
   ...
   1 as Month12,
   ID
from Data_Table 
where Year = 2012 and ID = 1

Disclaimer: not tested.

Community
  • 1
  • 1
dani herrera
  • 48,760
  • 8
  • 117
  • 177