function DBwriteIPLHR(DATAinsert,idElabData,ListaDate,NodeNum,DTcatena,IDcentralina,conn)

nNodo = num2str(NodeNum);
l = ListaDate;

for ii=1:l
    idData = idElabData(ii,1);
    if  idData == 0 % Se la data non è su DB (=0), scrivo i dati
        tablename = 'ELABDATADISP';
        checkA = isnan(cell2mat(DATAinsert(ii,19)));
        if checkA == 1 % il vettore accelerazione è NaN
           checkV = isnan(cell2mat(DATAinsert(ii,17)));
           if checkV == 1 % il vettore velocità è NaN         
                colnames = {'UnitName','ToolNameID','NodeNum','EventDate','EventTime',...
                    'X','Y','Z','XShift','YShift','ZShift','HShift','HShift_local',...
                    'HShiftDir','T_node','calcerr','AlfaX','AlfaY'};
                data = [DATAinsert(ii,1),DATAinsert(ii,2),DATAinsert(ii,3),...
                    DATAinsert(ii,4),DATAinsert(ii,5),DATAinsert(ii,6),...
                    DATAinsert(ii,7),DATAinsert(ii,8),DATAinsert(ii,9),...
                    DATAinsert(ii,10),DATAinsert(ii,11),DATAinsert(ii,12),...
                    DATAinsert(ii,13),DATAinsert(ii,14),DATAinsert(ii,15),...
                    DATAinsert(ii,20),DATAinsert(ii,21),DATAinsert(ii,22)];
           else % velocità è un numero
                colnames = {'UnitName','ToolNameID','NodeNum','EventDate','EventTime',...
                    'X','Y','Z','XShift','YShift','ZShift','HShift','HShift_local',...
                    'HShiftDir','T_node','speed','speed_local','calcerr','AlfaX','AlfaY'};
                data = [DATAinsert(ii,1),DATAinsert(ii,2),DATAinsert(ii,3),...
                    DATAinsert(ii,4),DATAinsert(ii,5),DATAinsert(ii,6),...
                    DATAinsert(ii,7),DATAinsert(ii,8),DATAinsert(ii,9),...
                    DATAinsert(ii,10),DATAinsert(ii,11),DATAinsert(ii,12),...
                    DATAinsert(ii,13),DATAinsert(ii,14),DATAinsert(ii,15),...
                    DATAinsert(ii,16),DATAinsert(ii,17),DATAinsert(ii,20),...
                    DATAinsert(ii,21),DATAinsert(ii,22)];
           end
        else % scrivo tutto
            colnames = {'UnitName','ToolNameID','NodeNum','EventDate','EventTime',...
                    'X','Y','Z','XShift','YShift','ZShift','HShift','HShift_local',...
                    'HShiftDir','T_node','speed','speed_local','acceleration',...
                    'acceleration_local','calcerr','AlfaX','AlfaY'};
                data = [DATAinsert(ii,1),DATAinsert(ii,2),DATAinsert(ii,3),...
                    DATAinsert(ii,4),DATAinsert(ii,5),DATAinsert(ii,6),...
                    DATAinsert(ii,7),DATAinsert(ii,8),DATAinsert(ii,9),...
                    DATAinsert(ii,10),DATAinsert(ii,11),DATAinsert(ii,12),...
                    DATAinsert(ii,13),DATAinsert(ii,14),DATAinsert(ii,15),...
                    DATAinsert(ii,16),DATAinsert(ii,17),DATAinsert(ii,18),...
                    DATAinsert(ii,19),DATAinsert(ii,20),DATAinsert(ii,21),DATAinsert(ii,22)];
        end
        fastinsert(conn,tablename,colnames,data);
    else % altrimenti li aggiorno
        tablename = 'ELABDATADISP';
        checkA = isnan(cell2mat(DATAinsert(ii,19)));
        if checkA == 1 % il vettore accelerazione è NaN
           checkV = isnan(cell2mat(DATAinsert(ii,17)));
           if checkV == 1 % il vettore velocità è NaN         
                colnames = {'UnitName','ToolNameID','NodeNum','EventDate','EventTime',...
                    'X','Y','Z','XShift','YShift','ZShift','HShift','HShift_local',...
                    'HShiftDir','T_node','calcerr','AlfaX','AlfaY'};
                data = [DATAinsert(ii,1),DATAinsert(ii,2),DATAinsert(ii,3),...
                    DATAinsert(ii,4),DATAinsert(ii,5),DATAinsert(ii,6),...
                    DATAinsert(ii,7),DATAinsert(ii,8),DATAinsert(ii,9),...
                    DATAinsert(ii,10),DATAinsert(ii,11),DATAinsert(ii,12),...
                    DATAinsert(ii,13),DATAinsert(ii,14),DATAinsert(ii,15),...
                    DATAinsert(ii,20),DATAinsert(ii,21),DATAinsert(ii,22)];
           else % velocità è un numero
                colnames = {'UnitName','ToolNameID','NodeNum','EventDate','EventTime',...
                    'X','Y','Z','XShift','YShift','ZShift','HShift','HShift_local',...
                    'HShiftDir','T_node','speed','speed_local','calcerr','AlfaX','AlfaY'};
                data = [DATAinsert(ii,1),DATAinsert(ii,2),DATAinsert(ii,3),...
                    DATAinsert(ii,4),DATAinsert(ii,5),DATAinsert(ii,6),...
                    DATAinsert(ii,7),DATAinsert(ii,8),DATAinsert(ii,9),...
                    DATAinsert(ii,10),DATAinsert(ii,11),DATAinsert(ii,12),...
                    DATAinsert(ii,13),DATAinsert(ii,14),DATAinsert(ii,15),...
                    DATAinsert(ii,16),DATAinsert(ii,17),DATAinsert(ii,20)...
                    ,DATAinsert(ii,21),DATAinsert(ii,22)];
           end
        else % scrivo tutto
            colnames = {'UnitName','ToolNameID','NodeNum','EventDate','EventTime',...
                    'X','Y','Z','XShift','YShift','ZShift','HShift','HShift_local',...
                    'HShiftDir','T_node','speed','speed_local','acceleration',...
                    'acceleration_local','calcerr','AlfaX','AlfaY'};
                data = [DATAinsert(ii,1),DATAinsert(ii,2),DATAinsert(ii,3),...
                    DATAinsert(ii,4),DATAinsert(ii,5),DATAinsert(ii,6),...
                    DATAinsert(ii,7),DATAinsert(ii,8),DATAinsert(ii,9),...
                    DATAinsert(ii,10),DATAinsert(ii,11),DATAinsert(ii,12),...
                    DATAinsert(ii,13),DATAinsert(ii,14),DATAinsert(ii,15),...
                    DATAinsert(ii,16),DATAinsert(ii,17),DATAinsert(ii,18),...
                    DATAinsert(ii,19),DATAinsert(ii,20),DATAinsert(ii,21),DATAinsert(ii,22)];
        end
        whereclause = ['WHERE UnitName = ''' IDcentralina ''' and ToolNameID = ''' ...
            DTcatena ''' and NodeNum = ''' nNodo ''' and EventDate = ''' cell2mat(DATAinsert(ii,4))...
            ''' and EventTime = ''' cell2mat(DATAinsert(ii,5)) ''' '];
        update(conn,tablename,colnames,data,whereclause)
    end
end

end
