Unix 版は簡単なんだけど、どうも Windows のほうは cpan ではうまくインストールできなかった(make test あたりでこける)ので、ppm を使えってことで。
私の環境では 64bit 版の ActivePerl is Perl for Windows, Mac, Linux, AIX, HP-UX & Solaris | ActiveState が c:Perl64 に入ってます。
1. コマンドラインから ppm を起動
2. Perl Package Manager が起動するので、上の検索に「dbd-mysql」と入力。
3.検索が絞られるので、左上の mark for install でチェックした後「→」ボタンの run mark action でインストール
正常にインストールできたら、c:Perl64sitelibDBD の中に mysql.pm があることを確認。
■実行テスト
1 2 3 4 5 6 7 8 9 10 11 12 13 | se DBI ; $db = DBI->connect('DBI:mysql:database','user','pass'); $rows = $db->selectall_arrayref("SELECT id, name, tel, modified from store;"); @row = @$rows ; # 一度、配列に直す $cnt = @row ; # 行数を取得 print "count: $cntn" ; # 繰り返し処理 foreach $r ( @row ) { print "num: $r->[0] $r->[1]n" ; } # 直接参照するとき # print $rows->[0][3] ; |
fetch で回してもよいのだが、selectall_arrayref で一気に配列に取り込んだほうが楽かもしれない。
日本語は「????」になってしまうので、これをどうするかは不明。datetime 型もどうやって取るかわからず。
追記
UTF8にするには、
$db = DBI->connect(‘DBI:mysql:db’,’user’,’pass’,{‘mysql_enable_utf8’=>1});
で良い模様。