Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2003

    Unanswered: SELECT QUERY Error handling for Null return.

    I am using the following SELECT query to get some data from DB2 database with table name table FLORDER.

    ###ODBC DSN
    if(!($dbh = new Win32::ODBC($connect))) {
    print "Error Connecting to $connect\n";
    die("Error:" .Win32::ODBC::Error(). "\n");

    ###select query


    $dbh-> sql($cmd) && die ("Sql error: ".($dbh-> Error())[1]."\n");

    while($dbh-> FetchRow()) {

    undef %Data;
    %Data = $dbh->DataHash();
    $cust_id = $Data{"CUST_ID"};
    $order_no = $Data{"ORDER_NO"};
    $order_date = $Data{"ORDER_DATE"};
    $pono = $Data{"PO_NUM"};

    This code works fine if value of $OrdNo exists. I am wondering how I introduce a error handler and print a message if query doesnt find any records in database for the value of $OrdNo (In other words how to check if query returns a "NULL" value to some specifc value of $OrdNo)

    Please help me or let me know where I can find ODBC / Perl Documantation about it.

    I am running it on APache and WIN XP



  2. #2
    Join Date
    Jan 2004
    Buenos Aires

    Lightbulb a good example to base on

    This code is not mine, but is a good and working example of ODBC with perl and MySQL (or any other DB). It is also useful to grab RSS files from WWW.
    I need to study it more and use it in my site. But it works great in Win XP pro and Apache 2. (remember to register your ODBC DB at your Control Panel/ODBC file origins).
    You need to load some RSS - RDF http addresses in the second table in order to get it working as expected.
    To know more about these modules:
    Read the 'perldoc -m DBI' and all the other tutorials. See the examples. Using DBI or DBD you are able to connect to any DB brand without changing more that one line of code.
    I hope that it helps!


    =head1 NAME

    [] Reads RSS with ODBC (any DB)

    =head1 DESCRIPTION

    : ODBC : a System DSN named TESTSERVER :

    ExternalNewsSourceID -> autoNumber (internal unique id)
    Title -> text (name of the site)
    Link -> text (link to the site, not the source)
    Description -> text (description of the site)
    Source -> text (URL for the actual feed)

    My second table is named ExternalNews and has the following columns:

    SourceID -text
    PostDate -text
    Title -text
    Link -text
    Description -text


    use LWP::UserAgent;
    use XML::RSS;

    #We're running this off of a Windows machine, connecting to a M$SQL server
    # although any old SQL server would do (e.g. MySQL)

    use Win32::ODBC;


    #Create a new UserAgent to pull the XML data down
    $ua = new LWP::UserAgent;
    $ua->agent("HeadlineAgent/0.1 ".$ua->agent);

    #connect via ODBC to the SQL server
    if(!($db = new Win32::ODBC($DSN))){
    print "Error connecting to $DSN\n";
    print "Error: " . Win32::ODBC::Error() . "\n";

    # We'll be pulling in RSS files from various sources,
    # their URL's are stored in the SQL database

    my %sources;

    if($db->Sql("SELECT * FROM ExternalNewsSources"))
    print "SQL failed.\n";
    print "Error: " . $db->Error() . "\n";

    my(%data) = $db->DataHash();
    # ...process the data...
    # Add to hash of hashes
    $sources{$data{'ExternalNewsSourceID'}} = $data{'Source'};

    #Create the RSS object to parse the RSS files retrieved...
    my $rss = new XML::RSS;

    ($sec,$min,$hour,$mday,$mon,$year) = localtime(time);
    # preformatted string compatible with SQLServer's timestamp field
    $nowstring = sprintf("%02i/%02i/%i %02i:%02i:%02i",($mon+1),$mday,($year+1900),$hour, $min,$sec);

    #Walk through each of the XML sources
    foreach $sourceid(keys %sources)
    # fetch RSS file from the source's URL
    my $request = new HTTP::Request GET => $sources{$sourceid};
    my $result = $ua->request($request);

    # grok the RSS file retrieved

    # Step through all the links in the RSS
    for my $i (@{$rss->{items}})
    #Sometimes the RSS mis-parses and give us an empty item
    next if(length($i->{'title'}) <= 0);#skip it - it's empty...

    # Check to see if we've already seen this link from this source before...
    $db->Sql("SELECT * FROM ExternalNews WHERE SourceID=".$sourceid." AND Link = '".$i->{'link'}."'");
    #si ya existe, saltearlo...
    next; #skip it - it's here already...
    #Plunk it into the database
    $db->Sql("INSERT INTO ExternalNews (SourceID,PostDate,Title,Link,Description) VALUES ($sourceid,'$nowstring','".$i->{'title'}."','".$i->{'link'}."','".$i->{'description'}."')");
    # Nuke the current values in the object, it appears that the XML lib recycles the variables without clearing them...
    $i->{'title'} = '';
    $i->{'link'} = '';
    $i->{'description'} = '';
    print "Doh! couldnt get ".$sources{$sourceid}.": $!\n";

    #clean up

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts