PEPPLER.ORG
Michael Peppler
Sybase Consulting
Menu
Home
Sybase on Linux
Install Guide for Sybase on Linux
General Sybase Resources
General Perl Resources
Freeware
Sybperl
Sybase::Simple
DBD::Sybase
BCP Tool
Bug Tracker
Mailing List Archive
Downloads Directory
FAQs
Sybase on Linux FAQ
Sybperl FAQ
Personal
Michael Peppler's resume

sybperl-l Archive

Up    Prev    Next    

From: Michael Peppler <mpeppler at MBAY dot NET>
Subject: Re: Sybase Column Names
Date: Oct 28 1997 12:33AM

1FA91-Rolf Kamp wrote:
> 
> Greetings:
>         I am writing a generic sybperl routine to read a sybase table and
>         create an HTML table out of the results, with the sybase table's
>         column names as the HTML table's headers.  The DBlib routine
>         dbcolsource seems to be what I need to get the sybase table's
>         columns names.  I attempt to use it as:
> 
> use Sybase::DBlib;
> $dbh=new Sybase::DBlib $user, $passwd, $db;
> $dbh->dbcmd("select * from blahblah");
> $dbh->dbsqlexec;
> $dbh->dbresults;
> $i=0;
> @ColNames = ();
> while ($ColNames[$i++] = $dbh->dbcolsource($i)) {
>          print "ColNames[$i-1]:$ColNames[$i-1]\n";
> }
> 
>         I get:
> Can't locate auto/Sybase/DBlib/dbcolsource.al in @INC (@INC contains: /opt/perl5/lib/PA-RISC1.1/5.00401 /opt/perl5/lib /opt/perl5/lib/site_perl/PA-RISC1.1 /opt/perl5/lib/site_perl .) at ./x.pl line 11
> 
>         Am I calling dbcolsource incorrectly, or do I need to modify @INC?

You're looking for dbcolname().
But you should be careful with the auto-increment inside of a 
compound statement:

	while ($ColNames[$i++] = $dbh->dbcolsource($i)) {

there's no real rule about exactly when $i gets incremented, ie
the value of $i passed to dbcolsource() might not be the one you expect.

Here's a subroutine that does what you want:

# Format sql output in an HTML3 table:

sub wsql
{
    my($db, $cmd, $title) = @_;
    my @res;
    my @data;
    my $flag = 1;
    
    if($db->{'MaxRows'}) {
	$db->dbsetopt(&DBROWCOUNT, "$db->{'MaxRows'}");
    }
    
    print "\n";
    $db->dbcmd($cmd);
    $db->dbsqlexec || return undef; # The SQL command failed
    
    while($db->dbresults != &NO_MORE_RESULTS) {
	if($flag) {
	    my $colid;
	    print "\n";
	    for($colid = 1; $colid <= $db->dbnumcols; ++$colid) {
		print "\n";
	    }
	    print "\n";
	    $flag = 0;
	}
        while (@data = $db->dbnextrow) {
	    print "\n";
	    foreach (@data) {
		print "\n";
	    }
	    print "\n";
        }
    }
    print "
$title
", $db->dbcolname($colid), "
$_
\n"; if($db->{'MaxRows'}) { $db->dbsetopt(&DBROWCOUNT, "0"); } } This sub also honors a MaxRow value, so that you can avoid having a query return 2 million rows by mistake to the browser... Michael -- Michael Peppler -||- Data Migrations Inc. mpeppler@datamig.com -||- http://www.mbay.net/~mpeppler