Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Can get values from db but cannot get related values from the relevant lookup table

by yoyomonkey (Initiate)
on Aug 15, 2006 at 12:45 UTC ( [id://567448]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings brothers and sisters.

On my Centos box i have connected my perl is now talking to an MSSQL server (using freetds and DBD::Sybase drivers) now i can connect to a database and access the information on the table three of the fields in the table is a numeric integer (which is a primary key in a different table which has the string value) I need to get the string value in the lookuptable instead of the integer on the primary table.
I have written this script but its not working as it tries to look for the bugState, BugPri and bugAssign in the database.
The script does work when i take these values out as the rest of the values refer to the database specifically and i can return them perfectly ....can anyone help.

The error message i get is
SELECT bugid,sTitle,bugpri,bugassign,bugstate FROM Bug WHERE bugid IN (23) DBD::Sybase::st execute failed: Server message number=207 severity=16 state=3 line=1 server=APWADEV01 text=Invalid column name 'bugid'. Server message number=207 severity=16 state=3 line=1 server=APWADEV01 text=Invalid column name 'bugpri'. Server message number=207 severity=16 state=3 line=1 server=APWADEV01 text=Invalid column name 'bugassign'. Server message number=207 severity=16 state=3 line=1 server=APWADEV01 text=Invalid column name 'bugstate'. Server message number=207 severity=16 state=3 line=1 server=APWADEV01 text=Invalid column name 'bugid'. at /var/www/html/twiki/lib/TWiki/Plugins/FogbugzTablePlugin.pm line 125.
i know its looking at the database when it should be the indirect

SCRIPT HERE

# package TWiki::Plugins::FogbugzTablePlugin; use strict; use DBI; use vars qw( $connection ); # cached connection # DBI specification of the connection to the database my $DB_PATH = "DBI:Sybase:server=192.168.70.56"; # DB user my $DB_USER = ''; # DB user's password my $DB_PASS = ''; # lookup i created my $indirect = { bugstate => { table => 'Status', index => 'ixStatus', string => 'sStatus' }, bugpri => { table => 'Priority', index => 'ixPriority', string => 'sPriority' }, bugassign => { table => 'Person_1', index => 'ixPerson', string => 'sFullName' } }; # TWiki ihook sub initPlugin { return 1; } sub commonTagsHandler { #my($text, $topic, $web ) = @_; # $_[0] refers to the first parameter to this function $_[0] =~ s/\bFog(\d+)/_link_to_bug($1)/ge; $_[0] =~ s/%FOG{(.*?)}%/_show_bugs($1)/ge; } # Connect to the DB sub _connect { my $this = shift; unless( $connection ) { # DBI->trace(10); $connection = DBI->connect( $DB_PATH, $DB_USER, $DB_PASS, { PrintError => 0, RaiseError => 1, }); } return $connection; } # Look up a string in another table, as indicated by the indirection # table in $indirect. If the string isn't indirected, simply return it +. # Note that this could also be done by compiling a smarter query, but +in # most cases this is quite fast enough, and demonstrates the principle # nicely. sub _lookup { my( $db, $fieldName, $row ) = @_; my $indy = $indirect->{$fieldName}; if( $indy ) { my $query = "SELECT $indy->{string} FROM $indy->{table} ". "WHERE $indy->{index}=$row->{$fieldName}"; my $sth = $db->prepare( $query ); eval { $sth->execute(); }; die "$query $@" if $@; my $row = $sth->fetchrow_hashref(); return $row->{$indy->{string}}; } else { return $row->{$fieldName}; } } sub _show_bugs { my $args = shift; my $headers = '| *ID* | *Title* | *Priority* | *Assignment* | *State* |'; my $format = '| fog$bugid | $sTitle | $bugpri | $bugassign | $bugstate |'; my $query = 'SELECT '; my $fieldsel = $format; my @fieldset; while( $fieldsel =~ s/\$(\w+)// ) { push( @fieldset, $1 ); } $query .= join(',', @fieldset ); $query .= " FROM Bug"; $query .= ' WHERE bugid IN ('.$args.')'; my $db = _connect(); my $sth = $db->prepare( $query ); eval { $sth->execute(); }; die "$query $@" if $@; my $result = $headers; my $table = "$headers\n"; while( my $row = $sth->fetchrow_hashref() ) { my $fields = $format; $fields =~ s/\$(\w+)/_lookup($db, $1, $row)/ge; $table .= "$fields\n"; } return $table; } sub _link_to_bug { my $bugid = shift; return '[[http://apwadev01/fogbugz/default.asp?'.$bugid.'][Fog'.$b +ugid.']]'; } 1;

Many thanks to all for all your help

  • Comment on Can get values from db but cannot get related values from the relevant lookup table
  • Download Code

Replies are listed 'Best First'.
Re: Can get values from db but cannot get related values from the relevant lookup table
by imp (Priest) on Aug 15, 2006 at 12:56 UTC
    The perl code in this case isn't really relevant as this is a SQL issue. You should confirm that you are querying the correct table, and that the columns you listed exist in that table.

    Try running the SQL in Query Analyzer if you have access to a windows machine.

Re: Can get values from db but cannot get related values from the relevant lookup table
by davidrw (Prior) on Aug 15, 2006 at 13:29 UTC
    Not sure about your primary question (though it seems like it's a db issue, not perl, from first glance of the error message) ... but you might be interested in SQL::Abstract
    use SQL::Abstract; my $sa = SQL::Abstract->new; # in _lookup(): my $query = "SELECT $indy->{string} FROM $indy->{table} ". "WHERE $indy->{index}=$row->{$fieldName}"; my ($sql, @bind) = $sa->select( $indy->{table}, [$indy->{string}], { + $indy->{index} => $row->{$fieldName} } ); my ($val) = $db->selectrow_array( $sql, {}, @bind ); return $val; # in _show_bugs(): my ($query,@bind) = $sa->select('Bug', \@fieldset, {bugid=>[split( +/,/,$args)]} ); my $sth = $db->prepare( $query ); eval { $sth->execute(@bind); };
    Hmm.. actually, rewriting both (and maybe this auotmagically fixes your problem?):
    sub _show_bugs(){ my $args = shift; my @headers = map { "*$_*" } qw/ ID Title Priority Assignment Stat +e /; my $fmt = "| %s | %s | %s | %s | %s |\n"; my $sql =<<EOF; SELECT 'fog' || b.bugid, b.sTitle, p.sPriority, u.sFullName, s.sStatus FROM Bug b LEFT JOIN Priority p ON p.ixPriority = b.bugpri LEFT JOIN Persion_l u ON u.ixPerson = b.bugassign LEFT JOIN Status s ON s.ixStatus = b.bugstate WHERE b.bugid IN ($args) EOF my $db = _connect(); my $rows = $db->fetchall_arrayref($sql); # note: is slurping in a +ll rows return join '', map { sprintf $fmt, @$_ } \@headers, @$rows; }
    This way should be much more efficient -- let the database do the lookup work for you instead of making a bunch of calls (note that _lookup() isn't needed)... It's also a lot clearer -- you can see just from reading the SQL what it's doing..
    note: you may or may not want LEFT joins.. (probably not, but i put them so as not to modify functionality)
    note: it would be better to do the $args w/placeholders
Re: Can get values from db but cannot get related values from the relevant lookup table
by cdarke (Prior) on Aug 15, 2006 at 16:41 UTC
    I'm pretty sure that error 207 is an invalid column name, so check the spelling.
Re: Can get values from db but cannot get related values from the relevant lookup table
by mpeppler (Vicar) on Aug 15, 2006 at 18:07 UTC
    The most likely problem is that the default database for the user is not the one where the table you want is located.

    Either use a fully qualified table name (of the form database_name..table_name) or use the database attribute in the connection DSN (in the DBI->connect() call).

    See the DBD::Sybase pod for more information on the connect() attributes that are supported and what they do.

    Michael

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://567448]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-24 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found