Ich habe eine Importfunktion(im Thread: import csv/xls auf zwei tabellen (mit schlüssel)?),
wo ich eine csv tabelle in die datenbank kopiere. Vorher muss die csv Datei sortiert werden, weil ja jede anders aussehen kann.Also generiere ich mir eine neue txt Datei, wo die Einträge mit TAB als Trennung in richtiger Reihenfolge stehen.
Nun habe ich einige nicht ausgefüllte Felder, wie muss ich diese behandeln?
Wenn ich einfach nur ein TAB hinsetze, bekomme ich solch einen Fehler:
ERROR: 22007: invalid input syntax for type date: “”, obwohl diese Spalte Not Null erlaubt ist.
Mit DBNull.value, passiert das selbe.
test=# create table nullwerte (id int, val int);
CREATE TABLE
test=*# \h copy
Command: COPY
Description: copy data between a file and a table
Syntax:
COPY tablename [ ( column [, ...] ) ]
FROM { 'filename' | STDIN }
[ [ WITH ]
[ BINARY ]
[ OIDS ]
[ DELIMITER [ AS ] 'delimiter' ]
[ NULL [ AS ] 'null string' ]
[ CSV [ HEADER ]
[ QUOTE [ AS ] 'quote' ]
[ ESCAPE [ AS ] 'escape' ]
[ FORCE NOT NULL column [, ...] ]
COPY tablename [ ( column [, ...] ) ]
TO { 'filename' | STDOUT }
[ [ WITH ]
[ BINARY ]
[ HEADER ]
[ OIDS ]
[ DELIMITER [ AS ] 'delimiter' ]
[ NULL [ AS ] 'null string' ]
[ CSV [ HEADER ]
[ QUOTE [ AS ] 'quote' ]
[ ESCAPE [ AS ] 'escape' ]
[ FORCE QUOTE column [, ...] ]
test=*# copy nullwerte from stdin null as '';
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> 1 1
>> 2
>> 3 3
>> \.
test=*# select * from nullwerte ;
id | val
----+-----
1 | 1
2 |
3 | 3
(3 rows)