データ更新
UPDATE
sample.pl
# プラグマ
use strict;
use warnings;
# DBI定義
use DBI;
########################################
# データ取得方法3
########################################
sub select_db
{
# パラメータ取得
my ($sth) = @_;
# レコード数を取得
my $num_rows = $sth->rows;
print "該当 $num_rows 件\n";
# ハッシュのリファレンスを取得
while ( my $hash_ref = $sth->fetchrow_hashref ){
print "id=$hash_ref->{code}, name=$hash_ref->{value} memo=$hash_ref->{biko} \n";
}
}
my $user = 'root';
my $passwd = '';
my $dbname = 'test01';
my $host = 'localhost';
my $db = DBI->connect("DBI:mysql:$dbname:$host", $user, $passwd);
# SELECT実行
my $sth = $db->prepare("select * from table01");
$sth->execute;
# 前取得
print "前データ\n";
select_db($sth);
print "\n";
# UPDATE実行
my $sth2 = $db->prepare("update table01 set value = 'zzzzz', biko = 'ZZZZZ' where code = 5");
$sth2->execute;
# SELECT実行
my $sth3 = $db->prepare("select * from table01");
$sth3->execute;
# 変更後取得
print "変更後データ\n";
select_db($sth3);
print "\n";
$sth->finish;
$db->disconnect;
0;
# 処理開始
BEGIN {
print "test-script-start\n";
}
# 処理終了
END {
print "test-script-end\n";
}
結果
>perl sample.pl
test-script-start
前データ
該当 4 件
id=1, name=aaaaa memo=
id=2, name=bbbbb memo=
id=3, name=ccccc memo=
id=5, name=ddddd memo=memo-data
変更後データ
該当 4 件
id=1, name=aaaaa memo=
id=2, name=bbbbb memo=
id=3, name=ccccc memo=
id=5, name=zzzzz memo=ZZZZZ
test-script-end
>
最終更新:2012年01月21日 19:24