All pastes #572937 Raw Edit

sql attempt

public text v1 · immutable
·published 2007-06-17 22:53 UTC
rendered paste body
sub sql_write {
  my $hash = shift;
  my $dbh = DBI->connect("dbi:SQLite:dbname=dbfile","","",
                          { RaiseError => 1, AutoCommit => 0 });
  eval {
    $dbh->do("DELETE FROM mahou_data");
    $dbh->commit;
  } if($@) {
    print "Failure: $@\n";
    $dbh->rollback and return;
  } 

  my $sth = $dbh->prepare("INSERT INTO mahou_data values(?,?,?,?,?)");
  eval {
    foreach my $anime (sort keys %{$hash}) {
      $sth->execute( $anime, @{[ map { $hash->{$anime}{$_} } ('link','station','airtime','eta') ]} );
    }
    $dbh->commit;
  } if($@) {
    print "Failure: $@\n";
    $dbh->rollback and return;
  }
  $dbh->disconnect;
}