DBIモジュールを使用してMySQLに接続後、SELECT文でデータを取得するサンプルコードを以下に記します。
使用環境はCentOS(Linux)で動作確認を行いました。
MySQLに接続しデータベース(db1)を作成しテーブル(t1)を作成したあと、INSERT文を使い数件のデータをインサートしています。
このデータベースとテーブルを使用しPerlのSELECTサンプルコードを実行します。
MySQLで実行したコマンドと出力は以下の通りです。
[sakura@centos6 ~]$ mysql -u root -s -p Enter password: mysql> create database db1; mysql> use db1; mysql> create table t1 (a int, b varchar(10)); mysql> insert into t1 values(1,'Sakura'),(2,'Tsubaki'),(3,'Kiku'); mysql> select * from t1; a b 1 Sakura 2 Tsubaki 3 Kiku mysql> \q
サンプルコード内になるデータベース名やユーザ、パスワードなどはみなさんの環境にあった値に変更してください。
fetchrow_arrayrefは、レコード情報を配列のリファレンスで返却します。
以下に2種類のサンプルコードを記します。
以下のサンプルは配列のリファレンスを@$で配列にし、my($a, $b)の変数に代入しています。
#!/usr/bin/perl
use strict;
use DBI;
# MySQL
our $DB_NAME = "db1";
our $DB_USER = "root";
our $DB_PASS = "mysql";
our $DB_HOST = "localhost";
our $DB_PORT = "3306";
my $dbh = DBI->connect("dbi:mysql:dbname=$DB_NAME;host=$DB_HOST;port=$DB_PORT","$DB_USER","$DB_PASS") or die "$!\n Error: failed to connect to DB.\n";
my $sth = $dbh->prepare("SELECT * FROM t1;");
$sth->execute();
while (my $ary_ref = $sth->fetchrow_arrayref) {
my ($a, $b) = @$ary_ref;
print "$a, $b\n";
}
$sth->finish;
$dbh->disconnect;
以下のサンプルは配列のリファレンスを->で参照しています。
#!/usr/bin/perl
use strict;
use DBI;
# MySQL
our $DB_NAME = "db1";
our $DB_USER = "root";
our $DB_PASS = "mysql";
our $DB_HOST = "localhost";
our $DB_PORT = "3306";
my $dbh = DBI->connect("dbi:mysql:dbname=$DB_NAME;host=$DB_HOST;port=$DB_PORT","$DB_USER","$DB_PASS") or die "$!\n Error: failed to connect to DB.\n";
my $sth = $dbh->prepare("SELECT * FROM t1;");
$sth->execute();
while (my $ary_ref = $sth->fetchrow_arrayref) {
print "$ary_ref->[0], $ary_ref->[1]\n";
}
$sth->finish;
$dbh->disconnect;
上記2つのサンプルコードを実行すると以下のような出力になります。
$ ./fetchrow_arrayref_1.pl 1, Sakura 2, Tsubaki 3, Kiku
$ ./fetchrow_arrayref_2.pl 1, Sakura 2, Tsubaki 3, Kiku
fetchrow_hashrefは、レコード情報をハッシュのリファレンスで返却します。
以下に2種類のサンプルコードを記します
以下のサンプルはハッシュのリファレンスをハッシュ変数に代入し$row{'a'}という感じで値を参照しています。
#!/usr/bin/perl
use strict;
use DBI;
# MySQL
our $DB_NAME = "db1";
our $DB_USER = "root";
our $DB_PASS = "mysql";
our $DB_HOST = "localhost";
our $DB_PORT = "3306";
my $dbh = DBI->connect("dbi:mysql:dbname=$DB_NAME;host=$DB_HOST;port=$DB_PORT","$DB_USER","$DB_PASS") or die "$!\n Error: failed to connect to DB.\n";
my $sth = $dbh->prepare("SELECT * FROM t1;");
$sth->execute();
while (my $hash_ref = $sth->fetchrow_hashref) {
my %row = %$hash_ref;
print "$row{a}, $row{b}\n";
}
$sth->finish;
$dbh->disconnect;
以下のサンプルはハッシュのリファレンスを->で参照しています。
#!/usr/bin/perl
use strict;
use DBI;
# MySQL
our $DB_NAME = "db1";
our $DB_USER = "root";
our $DB_PASS = "mysql";
our $DB_HOST = "localhost";
our $DB_PORT = "3306";
my $dbh = DBI->connect("dbi:mysql:dbname=$DB_NAME;host=$DB_HOST;port=$DB_PORT","$DB_USER","$DB_PASS") or die "$!\n Error: failed to connect to DB.\n";
my $sth = $dbh->prepare("SELECT * FROM t1;");
$sth->execute();
while (my $hash_ref = $sth->fetchrow_hashref) {
print "$hash_ref->{a}, $hash_ref->{b}\n";
}
$sth->finish;
$dbh->disconnect;
上記2つのサンプルコードを実行すると以下のような出力になります。
$ ./fetchrow_hashref_1.pl 1, Sakura 2, Tsubaki 3, Kiku
$ ./fetchrow_hashref_2.pl 1, Sakura 2, Tsubaki 3, Kiku