2012年8月8日 星期三

[Perl] Query MySQL + Set::Light

sub query() 
{
    use DBI;
    use Set::Light;

    my $data_source = q/dbi:mysql:database:ip:port/;
    my $username = q/username/;
    my $password = q/password/;

    my $db = DBI->connect($data_source, $username, $password) 
        or die "Cannot connect to $data_source: $DBI::errstr";

    my $sql = "select * from Table";

    my $stmt = $db->prepare($sql) 
        or die "Cannot prepare statement: $DBI::errstr";

    $stmt->execute();

    printf "%20s\n", $stmt->{NAME}->[0];

    my $set = Set::Light->new( qw/A B C D/ );

    while ( my @row = $stmt->fetchrow_array ) {
        printf "%20s\n", $row[0] if $set->has($row[1]);
    }

    $db->disconnect;
}

沒有留言:

張貼留言