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

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on How to download data from a table in Sybase and save the data in text file format using Perl?
  • Download Code

Replies are listed 'Best First'.
Re: How to download data from a table in Sybase and save the data in text file format using Perl?
by Abigail (Deacon) on Jun 08, 2001 at 20:31 UTC
    Trust me, you really, really, really do not want to roll your own table-to-file application - specially not if you are new to Perl.

    First, there is the utility bcp that comes with Sybase. You can use that. And if you really want to use Perl and not a seperate process, get yourself sybperl and use Sybase::BCP. The DBI isn't the only interface available to connect to databases, there are more, which don't restrict themselves to common lowest denominator.

    -- Abigail

Re: How to download data from a table in Sybase and save the data in text file format using Perl?
by ckohl1 (Hermit) on Jun 08, 2001 at 16:39 UTC
    One standout issue could be the existence of special characters (quotes (double, single), CRLF combinations, commas) within your data. These special characters may need to be transliterated (or escaped) before you dump to a file.

    Another point could be that there are already numerous utilities for Sybase with regard to bulk data moves and copies.

    dbunload.exe
    Sybase SQL's 'output to'
    Sybase SQL's 'unload to'

    But I am not here to dissuade you from performing your task using Perl.


    Chris
    'You can't get there from here.'
      Along with bcp.
Re: How to download data from a table in Sybase and save the data in text file format using Perl?
by riffraff (Pilgrim) on Jun 08, 2001 at 17:01 UTC
    Well, if you are just saving to a text file, why do you have those header() function calls at the beginning? Is it for a web page?

    That said, I would definitely change this:

    $str = $ctr .= $tb .= $total .= $tb .= $sensible .= $tb .= $model .= $ +nl;

    to this:

    $str=join($tb,$ctr,$total,$sensible,$model).$nl;

    Or you could get rid of the possibly unnecessary variables and do this:

    $str=join("\t",$ctr,$total,$sensible,$model)."\n";
      actually the header function is sth i saw in PHP...like this:
      if ($viewreport == "Download") { header( "Content-type: application/x-something" ); header( "Content-Disposition: attachment; filename=$type.txt" ); header( "Content-Description: PHP3 Generated Data" );
      these codes will cause the WIN98 Save As window pops out and i will be able to save the downloaded data as text files..but how can i do this using Perl? }
Re: How to download data from a table in Sybase and save the data in text file format using Perl?
by bwana147 (Pilgrim) on Jun 08, 2001 at 17:33 UTC

    Sorry, this is rather a comment than an answer. I'd like to come back on your $nl = "\r\n". HTTP specs want you to use CR+LF to end your lines. Your code works fine as long as your webserver runs on Un*x. If you try and run it on a Win32 machine (<troll>but why on earth would you do that? ;-) </troll>), then Perl will helpfully turn your \n into a CR+LF sequence, to adapt to the local custom. And you end up with CR+CR+LF, which is not exactly what you want.

    So when dealing with a network protocol where ASCII values are important, \r\n should be written \015\012, be it only for the sake of portability.

    --bwana147

      "\r\n" and "\015\012" in Perl will be handled the same everywhere except MacOS (and perhaps non-ASCII environments) so this practice is buying you little here. In particular, coding "\n" as "\012" does not stop Win32 from prepending a "\r" to it when output via a file handle that isn't in binmode.

              - tye (but my friends call me "Tye")