Weird Oracle CLOB insert problem

From: Mark Coleman (mcoleman_at_insteel.com)
Date: 05/28/04


Date: 28 May 2004 09:14:57 -0700

Hi,
   I'm working on some test code to insert and read clob and blob
objects from Oracle 9.2. I've got perl 5.8.4 and DBD-Oracle 1.15
installed. The code I am testing with is from the example page of the
DBD-Oracle documentation. I can run the code and it appears to work
with no problem, but when I look at the table in sqlplus I see 2 rows
inserted with the same lob_id, one with blank chardata and the other
with just a number in it. I did a select dbms_lob.getlength on the
data and one row is 0 and the other is 3 or 4 or 5, even though I put
multiple lines of text in the source file. If I just select the data
one row has a number in the chardata column and the other is empty.
Any help anyone can provide would be much appreciated.
Here is a copy of the code I was using. The sample table is
lob_example, lob_id number, chardata clob).

use strict;
use DBI;

my $dbh = DBI->connect('dbi:Oracle:edwtest',
                        'rep_user',
                        'rep_user') || die "Database connection not
made: $DBI::errstr";

print("Selecting next val\n");

my $lob_id = $dbh->selectrow_array( <<" SQL" );
      SELECT lob_example_seq.nextval FROM DUAL
   SQL

   my $sth = $dbh->prepare( <<" SQL" );
      INSERT INTO lob_example
      ( lob_id, chardata )
      VALUES ( ?, ? )
   SQL

print("Binding params\n");

use DBD::Oracle qw( :ora_types);

   $sth->bind_param(1,$lob_id);
   $sth->bind_param(2,'',{ ora_type => ORA_CLOB });

   $sth->execute();

   $sth->execute( $lob_id, '' );

print("Selecting lob for update\n");
   $sth = $dbh->prepare( <<" SQL", { ora_auto_lob => 0 } );
      SELECT chardata
      FROM lob_example
      WHERE lob_id = ?
      FOR UPDATE
   SQL
   $sth->execute( $lob_id );
   my ( $char_locator ) = $sth->fetchrow_array();
   $sth->finish();

print("Opening the file\n");
   open (CHAR_FH, "/oracle/perlstuff/test.txt") or die;
   my $chunk_size = 4096; # Arbitrary chunk size

   # BEGIN WRITING CHAR_DATA COLUMN
   my $offset = 1; # Offsets start at 1, not 0
   my $length = 0;
   my $buffer = '';
   while( $length = read( CHAR_FH, $buffer, $chunk_size ) ) {
      $dbh->ora_lob_write( $char_locator, $offset, $length );
      $offset += $length;
   }

print("Finished\n");
$sth->finish();
$dbh->disconnect();



Relevant Pages

  • Re: checking if text has changed
    ... clob. ... if the sql didn't return any rows, then i know that somebody change the ... When the user tries the save the data, you read the record again and compare what's held in memory against what was just read. ...
    (comp.programming)
  • Re: How to Pass Large Amounts of Data Through PL/SQL Procedure Arguments?
    ... > Either LOBs or LONG type arguments are not working when I pass them ... SQL> create or replace procedure foolob(big IN OUT NOCOPY CLOB) is ... PL/SQL procedure successfully completed. ...
    (comp.databases.oracle.server)
  • Re: Why all the max length constraints?
    ... implement the RM" in which field lengths are either fixed or ... some implementations of the RM had fixed or max lengths and others did ... they all implement some variety of SQL, ... Excuse me, but I've never used the CLOB data type, so my memory of it is ...
    (comp.databases.theory)
  • DBD::Oracle out of memory
    ... I have some problem with dbd-oracle 1.19 ... Its work fine if I use CLOB as a bind- variable in a loop. ... While($id < $maxid) ...
    (perl.dbi.users)
  • Re: XSU for PL/SQL or Java Question
    ... SQL> INSERT INTO my_table ... myclob CLOB; ... PL/SQL Release 10.1.0.2.0 - Production ... > what would col3 data look like in the returned XML document? ...
    (comp.databases.oracle.server)