2017-06-24 40 views
0

我發現下面的函數從csv複製數據到Postgres,它也從csv動態創建表,我想要類似的功能,但它應該做的文本文件。postgres功能:從txt文件複製到數據庫

我不是從開發背景,我必須加載一個txt文件到Postgres表動態。

是否可以使下面的功能與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:錯誤:語法錯誤在或附近「用戶」 其中:PL/pgSQL函數load_csv_file(文本,文本,整數)第29行EXECUTE CSV文件: 用戶,客戶風險,拒絕原因,應用程序狀態,日期,指標,應用程序數量,高風險應用程序數量 amcconnell7,最低,未拒絕,已啓動,2017年5月11日,2, amcconnell7,最低,未拒絕,已啓動,2017年5月12日,1,amcconnell7,最低,未拒絕,已啓動,2017年5月15日1, 爲了避免任何字符問題,我想我是否可以使用TXT代替csv – 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; 
相關問題