Thread: Execute A String of Query

Execute A String of Query

From
"lonh SENG"
Date:
Hi all,
 
    I am really new to Postgres. But I am familiar with SQL Server.
 
    I want import data from delimiter file.
    How can I do this?
    My delimiter file is as following:
 
uid|username
----+---------
2974|012801849
3883|012950864
6015|012990918
4512|012854963
(4 rows)
   
    Anyone has an idea?
   
    TIA
 
Regards,
 
lonh

Re: Execute A String of Query

From
Daniel Andersen
Date:
>     I am really new to Postgres. But I am familiar with SQL Server.
>
>     I want import data from delimiter file.
>     How can I do this?
>     My delimiter file is as following:
>
> Regards,
>
> lonh

I'm not sure if theres a neater solution out there, but I usually just pipe
it through a one liner perl script and then back into psql, sorta like

cat filename|perl -ne 's/\|/,/g; chop $_; print "insert into tablename values
($_);\n"'|psql databasename

under the bash shell. Of course this all gets a bit messy if there are
strings involved, but works nicely for numerical data. although the example
given suggests you have access to the original database, in which case simply
pg_dumping it is a much easier solution :)
    Daniel

Re: Execute A String of Query

From
Stephan Szabo
Date:
On Wed, 23 Jan 2002, lonh SENG wrote:

> Hi all,
>
>     I am really new to Postgres. But I am familiar with SQL Server.
>
>     I want import data from delimiter file.
>     How can I do this?
>     My delimiter file is as following:
>
> uid|username
> ----+---------
> 2974|012801849
> 3883|012950864
> 6015|012990918
> 4512|012854963
> (4 rows)
>
>     Anyone has an idea?

Well, if you trim off the header/footer info, you can probably
read the data using COPY.


Re: Execute A String of Query

From
"Nick Fankhauser"
Date:
Lon-

>    My delimiter file is as following:
>
>uid|username
>----+---------
>2974|012801849

Assuming all of these values are integers, and the file is called
/home/lon/users.txt, this should work in psql:


create table user_stuff (id Int, name BigInt);
copy user_stuff from '/home/lon/test.txt' using delimiters '|';


The filename must be the full unix path, and you'll nee to remove any
headers or footers that don't contain data.
see this chunk of the manual for more details:

http://www.postgresql.org/idocs/index.php?sql-copy.html

-Nick

--------------------------------------------------------------------------
Nick Fankhauser  nickf@ontko.com  Phone 1.765.935.4283  Fax 1.765.962.9788
Ray Ontko & Co.     Software Consulting Services     http://www.ontko.com/