Re: perl and insert

From: Harald Fuchs <use_reply_to(at)protecting(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: perl and insert
Date: 2005-05-17 21:06:47
Message-ID: pu3bslpno8.fsf@srv.protecting.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

In article <20050517161956(dot)GA23179(at)europa(dot)cosmos(dot)opusvl(dot)com>,
Rich Doughty <rich(at)opusvl(dot)com> writes:

> On 17 May 2005, Hrishikesh Deshmukh wrote:
>> Hi All,
>>
>> Anybody knows how to use perl dbi to read a file line by line and
>> insert into db!
>> The books which i have tell you exclusively on running queries.

> it depends on what you need to achieve, but a good place to start would be
> something like:

> while (my $line = <FILE>)
> {
> $dbh->do ('INSERT INTO table (line) VALUES (?)', undef, $line);
> }

> Where FILE is your open filehandle, and $dbh is your DBI connection, and
> you've modified the SQL as necessary.

> If performance is an issue, you may want to try this (although the
> performance gains depend on database you're using)

> my $st = $dbh->prepare ('INSERT INTO table (line) VALUES (?)');

> while (my $line = <FILE>)
> {
> $st->execute ($line);
> }

If there are many lines in the file, Hrishikesh might be better off
using COPY instead of INSERT. "perldoc DBD::Pg" says how to do that.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Sven Willenberger 2005-05-17 21:40:32 Re: CSV delim quoting differences PgCOPY, Excel etc...
Previous Message Gianni Mariani 2005-05-17 21:04:59 What should I do with this: invalid page header in block ... ?