14.17 CSVファイルをSQLで問い合わせる

SQLを使用してCSVファイルのデータの挿入、削除、または取り出しを行いたい。

  • CPANで公開されているDBD::CSVモジュールを使用する。
use DBI;

$dbh = DBI->connect("dbi:CSV:f_dir=/home/gnat/payroll", "", "",
  { AutoCommit => 1, RaiseError = > 1 });

$dbh->do("UPDATE salaries SET salary = salary * 2 WHERE name = 'NAT'");

$sth = $dbh->prepare("SELECT name, salary FROM salaries WHERE name = 'NAT'");
$sth->execute();
while (@row = $sth->fetchrow_array) {
  # ...
}
$sth->finish();

$dbh->disconnect();