2014-08-31 99 views
0

我如何告訴Postgres的,當最後一行已經導入停止進口數據?如何停止導入CSV數據?

我定義我的主鍵與NOT NULL約束。我的行結束於1452年。我得到的錯誤:

 ERROR: null value in column "coachiid" violates not-null constraint 
    DETAIL: Failing row contains (null, null, null, null, null, null, null, null, null, null, null). 
    CONTEXT: COPY coaches, line 1453: ";;;;;;;;;;" 

由於顯而易見的原因,postgres認爲它違反了constaints。然而,我的開放式辦公室csv表只是在那裏結束。有沒有一種方法可以表明這一點?

我想不是的Postgres其更OpenOffice的問題。也許有人有類似的經歷。

回答

0

PostgreSQL的複製的命令不能處理的約束違規。

你說得對,我覺得這是更好地考慮到之前的進口數據OpenOffice的問題和流程文件。或者使用臨時表。

但是,如果你prefere,如果我們能與所有字段設置爲null確定最後一行作爲行,就可以實現由觸發器忽略最後一行波紋管:

CREATE OR REPLACE FUNCTION cancel_if_empty_row() 
    RETURNS trigger AS 
$BODY$begin 

if tg_op='INSERT' and new.coachiid is null and new.field2 is null and new.field3 null and new.field4 is null then 
return null; 
end if; 

return new; 

end;$BODY$ 
    LANGUAGE plpgsql VOLATILE 
    COST 100; 


CREATE TRIGGER trigger_cancel_if_empty_row 
    BEFORE INSERT 
    ON my_table 
    FOR EACH ROW 
    EXECUTE PROCEDURE cancel_if_empty_row(); 

WIKI: COPY command

COPY command