0

csvからPostgresにデータをコピーする以下の関数が見つかりました。それはcsvから動的にテーブルを作成します。同様の機能が必要ですが、テキストファイルにする必要があります。postgres関数:txtファイルからデータベースへのコピー

私は開発の背景ではないので、動的にPostgresテーブルにtxtファイルをロードする必要があります。

以下の機能をtxtファイルで使用することは可能ですか?

create or replace function public.load_csv_file 
(
target_table text, 
csv_path text, 
col_count integer 
) 

returns void as $$ 

declare 

iter integer; -- dummy integer to iterate columns with 
col text; -- variable to keep the column name at each iteration 
col_first text; -- first column name, e.g., top left corner on a csv file or  spreadsheet 

begin 
set schema 'public'; 

create table insert_from_csv(); 

-- add just enough number of columns 
for iter in 1..col_count 
loop 
    execute format('alter table insert_from_csv add column col_%s text;', iter); 
end loop; 

-- copy the data from csv file 
execute format('copy insert_from_csv from %L with delimiter '','' quote ''"'' csv ', csv_path); 

iter := 1; 
col_first := (select col_1 from insert_from_csv limit 1); 

-- update the column names based on the first row which has the column names 
for col in execute format('select unnest(string_to_array(trim(temp_table::text, ''()''), '','')) from temp_table where col_1 = %L', col_first) 
loop 
    execute format('alter table insert_from_csv rename column col_%s to %s', iter, col); 
    iter := iter + 1; 
end loop; 

-- delete the columns row 
execute format('delete from insert_from_csv where %s = %L', col_first, col_first); 

-- change the temp table name to the name given as parameter, if not blank 
if length(target_table) > 0 then 
    execute format('alter table insert_from_csv rename to %I', target_table); 
end if; 

end; 

$$ language plpgsql; 

私は専門家の助けを借りて多くの助けになります。

+0

おそらく区切り文字と書式を変更するだけで十分です。テキストファイルのカラムセパレータは何ですか? –

+2

CSVファイル*は*テキストファイルです。あなたの質問は何ですか*? –

+0

@ ErwinBrandstetter - csvファイルからロードしようとしたとき - org.postgresql.util.PSQLException:エラー: "User"またはその近くの構文エラー ここで:PL/pgSQL関数load_csv_file(text、text、integer)行29 EXECUTE CSVファイル: ユーザー、お客様のリスク、申し立てられた理由、アプリケーションのステータス、日付、指標、アプリケーション数、リスクの高いアプリケーション amcconnell7、最低、未承認、開始日、2017年5月11日、2: amcconnell7、最低、未承認、開始、12月5月-2017、1、 amcconnell7、最低、未承認、開始日、2017年5月15日、1、 文字の問題を避けるために、 csvの代わりにtxtを使用する – Geeme

答えて

0

Postgresは最初の文字の最初の文字に問題があります。大文字の場合はエラーが発生し、小文字でうまくいきます。

ありがとうございましたerwin & michelが応答しました。

0

私はこの関数を変更することができました。これは '_'でスペースを置き換え、列名を小文字に置き換えます。

create or replace function load_csv_file 
(
target_table text, 
csv_path text, 
col_count integer 
) 

returns void 
SECURITY DEFINER 
as $$ 

declare 

iter integer; -- dummy integer to iterate columns with 
col text; -- variable to keep the column name at each iteration 
col_first text; -- first column name, e.g., top left corner on a csv file or  spreadsheet 

begin 
set schema 'test'; 

create table insert_from_csv(); 

-- add just enough number of columns 
for iter in 1..col_count 
loop 
execute format('alter table insert_from_csv add column col_%s text;', iter); 
end loop; 

-- copy the data from csv file 
execute format('copy insert_from_csv from %L delimiter '','' csv ',  csv_path); 

iter := 1; 
col_first := (select col_1 from insert_from_csv limit 1); 

-- update the column names based on the first row which has the column names 
for col in execute format('select unnest(string_to_array(lower(replace(trim(insert_from_csv::text, ''()''),'' '',''_'')), '','')) from insert_from_csv where col_1 = %L', col_first) 
loop 
execute format('alter table insert_from_csv rename column col_%s to %s', iter, col); 
iter := iter + 1; 
end loop; 

-- delete the columns row 
execute format('delete from insert_from_csv where %I = %L', col_first, col_first); 

-- change the temp table name to the name given as parameter, if not blank 
if length(target_table) > 0 then 
execute format('alter table insert_from_csv rename to %I', target_table); 
end if; 

end; 

$$ language plpgsql; 
関連する問題