SQLite Forum

How can I fill a NULL with previous Data ?
Login
That is only a problem if you set a value to 0 when there is no previous value.  However, you could define the table and some triggers so that it is impossible for the contents of the table to be invalid:

```
create table data
(
    Unix_Time   integer primary key,
    Day         text not null,
    Date        text not null,
    Time        text not null,
    ip_24_temperature   real,
    ip_24_humidity      real
) without rowid;

create trigger insert_data_validity before insert on data
begin
    select raise(ABORT, 'Cannot insert invalid data')
     where (new.ip_24_temperature is null and new.ip_24_humidity is null)
        or new.Unix_Time <= (
                             select max(Unix_Time)
                               from data
                            );
end;

create trigger update_data_validity_temperature before update of ip_24_temperature on data when old.ip_24_temperature is not null
begin
    select raise(ABORT, 'Cannot update data.ip_24_temperature');
end;

create trigger update_data_validity_humidity before update of ip_24_humidity on data when old.ip_24_humidity is not null
begin
    select raise(ABORT, 'Cannot update data.ip_24_humidity');
end;

create trigger update_data_validity_unixtime before update of unix_time on data
begin
    select raise(ABORT, 'Cannot update data.unix_time');
end;

create trigger insert_data_null_temperature after insert on data when new.ip_24_temperature is null
begin
    update data
       set ip_24_temperature = (
                                   select ip_24_temperature
                                     from data
                                   where Unix_Time < new.Unix_Time
                                     and ip_24_temperature is not null
                                order by Unix_Time desc
                                   limit 1
                               )
     where Unix_Time = new.Unix_Time;
end;

create trigger insert_data_null_humidity after insert on data when new.ip_24_humidity is null
begin
    update data
       set ip_24_humidity = (
                                select ip_24_humidity
                                  from data
                                where Unix_Time < new.Unix_Time
                                  and ip_24_humidity is not null
                             order by Unix_Time desc
                                limit 1
                            )
     where Unix_Time = new.Unix_Time;
end;
```