From: | Sean Davis <sdavis2(at)mail(dot)nih(dot)gov> |
---|---|
To: | Deepblues <deepblues(at)gmail(dot)com> |
Cc: | pgsql-novice(at)postgresql(dot)org, KeithW(at)narrowpathinc(dot)com |
Subject: | Re: Import csv file into multiple tables in Postgres |
Date: | 2005-03-03 20:48:23 |
Message-ID: | a6fd91207f4b6e4e3195ea8a088d73ee@mail.nih.gov |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-novice |
On Mar 3, 2005, at 3:10 PM, Deepblues wrote:
> Thanks for all that information, My scenario looks like this ...
>
> I need to import an excel spreadsheet into the postgresql database .
> I converted the excel spreadsheet into a csv file and now I have 3
> tables in my database where I need to import this data. Im new to both
> perl and postgres. do you have any sample script that I can have a
> look at which loads the csv file into a interface table ?
>
Just an example....
It will pay to read the DBI manual if you are going to use perl like
this. You can probably just use the psql \copy command, though.
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
my $dbh = DBI->connect('dbi:Pg:dbname=qiagen2;host=localhost',
'username',
'password',
{AutoCommit => 1},
);
open (IN,'<yourdatafile.txt');
my $sql = qq{CREATE TABLE g_rif (
g_rif_id serial primary key,
gene_id integer,
rif text not null,
pmid integer not null
)};
$dbh->do($sql);
$sql = qq{COPY g_rif (gene_id,rif,pmid) from STDIN};
my $sth = $dbh->prepare($sql);
$sth->execute() || die $sth->errstr;
while (<IN>) {
chomp;
my @params=split/,/;
# next line does the insertion, but with columns rearranged for
convenience....
my $ret =
$dbh->func(join("\t",$params[1],$params[4],$params[2])."\n",
'putline');
}
$dbh->func('endcopy');
close IN;
Sean
From | Date | Subject | |
---|---|---|---|
Next Message | Bruno Wolff III | 2005-03-03 21:35:47 | Re: Form Design Advice |
Previous Message | Deepblues | 2005-03-03 20:10:58 | Re: Import csv file into multiple tables in Postgres |