Re: CSV hack

Поиск
Список
Период
Сортировка
Искать
От
Andrew Dunstan
Тема
Re: CSV hack
Дата
Msg-id
3FD7EC2A.6020903@dunslane.net
Ответ на
CSV hack (David Fetter)
Список
Дерево обсуждения
CSV hack David Fetter <david@fetter.org>
Re: CSV hack Joe Conway <mail@joeconway.com>
Re: [Dbdpg-general] Re: CSV hack David Fetter <david@fetter.org>
Re: CSV hack Andrew Dunstan <andrew@dunslane.net>
Re: CSV hack david@fetter.org (David Fetter)

You also need to quote values containing the separator.

cheers

andrew (who used to set creating CSV as a programming exercise - 
students almost never get it right)

David Fetter wrote:

>Kind people,
>
>I've come up with yet another little hack, this time for turning 1-d
>arrays into CSV format.  It's very handy in conjunction with the
>array_accum aggregate (can this be made a standard aggregate?) in
>.
>
>Here 'tis...
>
>CREATE OR REPLACE FUNCTION csv(anyarray) RETURNS TEXT AS
>'DECLARE
>    in_array ALIAS FOR $1;
>    temp_string TEXT;
>    quoted_string TEXT;
>    i INTEGER;
>BEGIN
>    FOR i IN array_lower(in_array, 1)..array_upper(in_array, 1)
>    LOOP
>        IF in_array[i]::TEXT ~ ''"''
>        THEN
>            temp_string := ''"'' || replace(in_array[i]::TEXT, ''"'', ''""'')  || ''"'';
>        ELSE
>            temp_string := in_array[i]::TEXT;
>        END IF;
>        IF i = array_lower(in_array, 1)
>        THEN
>            quoted_string := temp_string;
>        ELSE
>            quoted_string := quoted_string || '','' || temp_string;
>        END IF;
>    END LOOP;
>    RETURN quoted_string;
>END;
>' LANGUAGE 'plpgsql';
>
>Those DBD::Pg users among us who'd like to be able to bind_columns to
>postgresql arrays may have a leg up with Text::CSV_XS.
>
>Other middleware should be able to handle such things, too. :)
>
>Cheers,
>D
>  
>


В списке pgsql-hackers по дате отправления
От: David Fetter
Дата:
От: david@fetter.org (David Fetter)
Дата:
Сообщение: Re: CSV hack
FAQ