書き直し

前に書いたスクリプトが使っていて不便に思うので、ちょろっと書き直し。

use strict;
use warnings;

use DBI;
use Encode qw/from_to/;

my $dbh = DBI->connect('dbi:SQLite:./foo.db', '', '', {RaiseError => 1}) or die $!;
my $sth = $dbh->prepare('SELECT * FROM table WHERE value LIKE ?');

my %name;
while (@ARGV) {
    my $key = shift @ARGV;
    from_to($key, 'cp932', 'utf-8');
    $sth->execute("%$key%");
    while (my ($foo, $bar) = $sth->fetchrow_array) {
        from_to($foo, 'utf-8', 'cp932');
        push @{$name{$bar}}, $foo;
    }
}

$sth->finish;
undef $sth; # DBD::SQLite 1.13 bug

foreach (keys %name) {
    print "$_\n";
    print "  $_\n" foreach sort @{$name{$_}};
}

$dbh->disconnect;