amonotod has asked for the wisdom of the Perl Monks concerning the following question:

Hey all,
I have a script I've been working on to export database tables to flat files.
Since one of the target DB's is Access, I've had to cobble some Win32::OLE code
together, using Win32::OLE::Const 'Microsoft ActiveX Data Objects';. Every-
thing seems to work, provided that none of the return fields go over 255 Chars.
What I get if they do is up to 255 chars of output data, appended by up to 20
chars of ENV data. Any ideas?

Thanks,
amonotod
amonotod@charter.net

Update -- code included
my %tables; my $gettablelist = 'MDBTables.exe -d"'. $db_name .'"'; #external exe, gets table info from access my @tableslist = split("\n", `$gettablelist`); my $db_dir = substr($db_name, index($db_name, "\\", -1), index($db_n +ame, ".")) ."_export"; print "output dir is $db_dir\n"; mkdir $db_dir; foreach my $tableinfo (@tableslist) { my (@data, $tablename, $tablecols); @data = split('--', $tableinfo); my @columns = split(",", $data[1]); $tablename = $data[0]; my @orderedcols; foreach (@columns) { my ($col_name, $col_pos) = split(" ", $_); $orderedcols[$col_pos - 1] = $col_name; } my $selectstatement = "select ("; my $count = 0; foreach (@orderedcols) { $selectstatement = $selectstatement ." ". $_; if ($count < $#orderedcols) { $selectstatement = $selectstatement ." &'|'&"; } elsif ($count == $#orderedcols) { $selectstatement = $selectstatement ." &'|'"; } $count++; } $selectstatement = $selectstatement .") AS TextReturn from [$tablena +me];"; $tables{$tablename} = $selectstatement; print "$selectstatement\n"; } use Win32::OLE; use Win32::OLE::Const 'Microsoft ActiveX Data Objects'; my $DSN = "PROVIDER='Microsoft.Jet.OLEDB.4.0';User ID=Admin;Password +='';Data Source=$db_name;" ."Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Wit +hout Replica Repair=False;Jet OLEDB:SFP=False;"; foreach my $table (sort keys %tables) { my $output_file = $db_dir ."\\". $table .".dat"; open (OUTFILE, "> $output_file"); my $SQL = $tables{$table}; print "$table\n"; my $Conn = Win32::OLE->new("ADODB.Connection"); $Conn->Open($DSN); my $RS = Win32::OLE->new("ADODB.Recordset"); $RS->Open($SQL, $Conn); until ($RS->EOF) { my $value = $RS->Fields("TextReturn")->value; print OUTFILE "$value\n"; $RS->MoveNext; } $RS->Close; $Conn->Close; close OUTFILE; print "Done with $table\n"; }

Replies are listed 'Best First'.
Re: Win32::OLE, cuts off at 255 Chars...
by bmann (Priest) on Jan 30, 2004 at 22:31 UTC
    You don't show any code, so I'm going to take a guess.

    If you're using the DBI, check the docs for LongReadLen. Here's an example. It gets the data from an Access db using ODBC instead of ADO. Several fields have a lenth of 1500.

    my $datasource = "driver=Microsoft Access Driver (*.mdb);dbq=c:\\s\\database.mdb"; my $username = 'admin'; my $password = ''; my $dbh = DBI->connect( "dbi:ODBC:$datasource", $username, $password, { LongReadLen => 1500, RaiseError => 1 }) || die "Error connecting: $!";

    HTH

      Unfortunately, I'm only able to use whatever comes standard with ActiveState
      5.8 or 5.6. I'm not allowed to force our "customers" (other internal groups)
      to use a non-standard-distribution module...

      Thanks,
      amonotod
Re: Win32::OLE, cuts off at 255 Chars...
by BrowserUk (Patriarch) on Jan 31, 2004 at 00:51 UTC

      Hi,
      This isn't a bug in Perl, it's a Win32 problem. a "String" in the Win32 world is (up to) 255 characters terminated by \0 (as in a C string). You'll find this is a limitation of a "Text" field in MS Access, too.

      You can also demonstrate this "environment variable feature" in IIS if you enter a key=value in a query string where the value has more than 255 characters... it gets chopped and Env variables get stuck in... Neat!

      HTH - Mark