pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: tjk@tksoft.com <tjk@tksoft.com>
To: jeff@hub.org
Cc: pgsql-sql@postgreSQL.org
Subject: Re: your mail
Date: Thu, 16 Sep 1999 12:11:17 -0700 (PDT)
Message-ID: <199909161911.MAA01273@uno.tksoft.com> (raw)
In-Reply-To: <Pine.LNX.4.10.9909161129070.27527-100000@penguin.hub.org>

Jeff,
The builtin functions lo_import and lo_export are
for files only. Because of the 8k limit, it wouldn't
be possible to insert larger values.

You need to create your own routine which stores the value in a 
file and then imports/exports it.

Something like ...
(not tested, just for an idea.)

sub writetofile {
	my $data = shift;
	my $tmpfile = &newtmpfile || return "";
	open (FILE, ">$tmpfile") || return "";
	print FILE $data;
	close (FILE);	
	return $tmpfile;
}

Then replace the following lines

> my $statement = $dbh->prepare("INSERT INTO test values('something',lo_import($largestring));");
>    $statement->execute();
>    $statement->finish();
> 
> $dbh->commit;
> $dbh->disconnect;

with 
 
my $largestring = &writetofile($largestring) || die("bad data.");
my $statement = $dbh->prepare("INSERT INTO test values('something',lo_import($largestring));");
    $statement->execute();
    $statement->finish();
 
 $dbh->commit;
 $dbh->disconnect;
unlink $largestring;
 
exit (0);


Troy


> 
> Got a presumably very quick question.
> 
> I'm writing a perl script. lets say i have a variable that's > 8k
> , for arguments sake it's 64k.
> 
> How can i insert this into a database, do i need large objects ?
> and if so, do i use the lo_import ? for some reason it seems that 
> this is only for files..
> 
> below is what i have but it doesn't seem to work..
> 
> jeff=> \d test
> Table    = test
> +----------------------------------+----------------------------------+-------+
> |              Field               |              Type                | Length|
> +----------------------------------+----------------------------------+-------+
> | blurb                            | text                             |   var |
> | message                          | oid                              |     4 |
> +----------------------------------+----------------------------------+-------+
> 
> ***************** code ********************
> 
> #!/usr/bin/perl
> 
> use strict;
> use DBI;
> 
> 
> my $dbh = DBI->connect("dbname=narf host=narf port=narf",'narf','','Pg',{'AutoCommit' => 0});
> if (!$dbh) {
>         exit print "Could not connect: $DBI::errstr\n";
> }
> 
> 
> ## lets create the large value here...
> ## this string is 64K (2^16)
> 
> 
> my $largestring = 'x';
> for (my $i=0;$i < 16 ;$i++) {
>         $largestring = $largestring . $largestring;
>         }
> 
> 
> my $statement = $dbh->prepare("INSERT INTO test values('something',lo_import($largestring));");
>    $statement->execute();
>    $statement->finish();
> 
> $dbh->commit;
> $dbh->disconnect;
> 
> thanks for the help.
> 
> ======================================================
> Jeff MacDonald
> 	jeff@hub.org	webpage: http://hub.org/~jeff
> 	jeff@pgsql.com	irc: bignose on EFnet
> ======================================================
> 
> 
> ************
> 
> 




view thread (3+ messages)

Message-ID: <199909161911.MAA01273@uno.tksoft.com>
Permalink:  ../199909161911.MAA01273@uno.tksoft.com/
Also on:    postgresql.org/message-id/199909161911.MAA01273@uno.tksoft.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tjk@tksoft.com, jeff@hub.org
  Subject: Re: your mail
  In-Reply-To: <199909161911.MAA01273@uno.tksoft.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox