Yanor.net/
Wiki
Blog
GitHub
Sandbox
開始行:
* SELECT [#ae945703]
$sth = $dbh->prepare('SELECT id, name, age FROM t1 WHERE...
$sth->execute(15);
while ($row = $sth->fetchrow_hashref()){
print $row->{id},"\n";
print $row->{name},"\n";
}
die $sth->errstr if $sth->err;
-fetchrow_xxx()が途中でエラーを起こした場合、返値がundef...
-全行走査できたのかエラーだったのか調べるためには、
-$sth->errを使うか、
-RaiseErrorで例外をキャッチする。
** selectxxx_xxx系 [#wc674cf2]
(prepare()を経ずに)SQLを直接実行する。
*** 1行を取得 [#c2963bd2]
$row = $dbh->selectrow_arrayref('SELECT * FROM t1'); # $...
$row = $dbh->selectrow_hashref( 'SELECT * FROM t1'); # $...
*** 全行を配列の配列(もしくはハッシュの配列)で取得 [#w3...
$rows = $dbh->selectall_arrayref("SELECT * FROM t1"); ...
$rows = $dbh->selectall_arrayref("SELECT * FROM t1",{Sli...
$rows = $dbh->selectall_arrayref("SELECT * FROM t1",{Sli...
$rows = $dbh->selectall_arrayref("SELECT * FROM t1",{Col...
$rows = $dbh->selectall_arrayref("SELECT * FROM t1",{Sli...
$rows = $dbh->selectall_arrayref("SELECT * FROM t1",{Col...
...
$rows = $dbh->selectall_arrayref("SELECT * FROM t1",{Col...
*** 全行をハッシュのハッシュで取得 [#j2638e9c]
$rows = $dbh->selectall_hashref("SELECT * FROM t1",1); #...
$rows = {
'1' => {'name' => 'taro','id' => '1','age' => '...
'3' => {'name' => 'hanako','id' => '3','age' =>...
'2' => {'name' => 'jiro','id' => '2','age' => '...
};
$rows = $dbh->selectall_hashref("SELECT * FROM t1",['id'...
$rows = {
'1' => {'taro' => {'name' => 'taro','id' => '1'...
'3' => {'hanako' => {'name' => 'hanako','id' =>...
'2' => {'jiro' => {'name' => 'jiro','id' => '2'...
};
** fetchxxx_xxx系 [#d6acc21f]
prepare()とexecute()を実行した後に行う。
*** 1行を取得 [#qe945f62]
@row = $sth->fetchrow_array(); # 配列
$row = $sth->fetchrow_arrayref(); # 配列のリファ...
$row = $sth->fetch(); # 配列のリファ...
$row = $sth->fetchrow_hashref(); # ハッシュのリ...
$row = $sth->fetchrow_hashref('NAME_uc'); # 上とほぼ同じ...
*** 全行を取得 [#pe489866]
$rows = $sth->fetchall_arrayref(); ...
$rows = $sth->fetchall_arrayref([0]); ...
$rows = $sth->fetchall_arrayref([-2,-1]); ...
$rows = $sth->fetchall_arrayref({}); ...
$rows = $sth->fetchall_arrayref({id=>undef,name=>undef})...
** プレースホルダーに値を渡す [#r9e0243d]
selectxxx_xxx系でもfetchxxx_xxx系でも、引数の最後の方(( ...
$row = $dbh->selectrow_hashref( 'SELECT * FROM t1 WHERE ...
** 高速なSELECT [#v77e0889]
fetchrow_arrayrefとbind_columnsを使う。
$sth->bind_columns(\($num, $str));
while ($row = $sth->fetchrow_arrayref){
print "$num,$str\n";
}
** 影響した行数 [#jacd6b3d]
$sth->rowsはすべてfetchし終わった後しか使えない。よって、...
** 値のエスケープ [#xf9b9469]
$data = $dbh->quote("don't");
$sql = "SELECT * FROM tbl WHERE name = $data";
http://search.cpan.org/perldoc?DBI#quote
** 参考 [#f5e07016]
http://search.cpan.org/perldoc?DBI
終了行:
* SELECT [#ae945703]
$sth = $dbh->prepare('SELECT id, name, age FROM t1 WHERE...
$sth->execute(15);
while ($row = $sth->fetchrow_hashref()){
print $row->{id},"\n";
print $row->{name},"\n";
}
die $sth->errstr if $sth->err;
-fetchrow_xxx()が途中でエラーを起こした場合、返値がundef...
-全行走査できたのかエラーだったのか調べるためには、
-$sth->errを使うか、
-RaiseErrorで例外をキャッチする。
** selectxxx_xxx系 [#wc674cf2]
(prepare()を経ずに)SQLを直接実行する。
*** 1行を取得 [#c2963bd2]
$row = $dbh->selectrow_arrayref('SELECT * FROM t1'); # $...
$row = $dbh->selectrow_hashref( 'SELECT * FROM t1'); # $...
*** 全行を配列の配列(もしくはハッシュの配列)で取得 [#w3...
$rows = $dbh->selectall_arrayref("SELECT * FROM t1"); ...
$rows = $dbh->selectall_arrayref("SELECT * FROM t1",{Sli...
$rows = $dbh->selectall_arrayref("SELECT * FROM t1",{Sli...
$rows = $dbh->selectall_arrayref("SELECT * FROM t1",{Col...
$rows = $dbh->selectall_arrayref("SELECT * FROM t1",{Sli...
$rows = $dbh->selectall_arrayref("SELECT * FROM t1",{Col...
...
$rows = $dbh->selectall_arrayref("SELECT * FROM t1",{Col...
*** 全行をハッシュのハッシュで取得 [#j2638e9c]
$rows = $dbh->selectall_hashref("SELECT * FROM t1",1); #...
$rows = {
'1' => {'name' => 'taro','id' => '1','age' => '...
'3' => {'name' => 'hanako','id' => '3','age' =>...
'2' => {'name' => 'jiro','id' => '2','age' => '...
};
$rows = $dbh->selectall_hashref("SELECT * FROM t1",['id'...
$rows = {
'1' => {'taro' => {'name' => 'taro','id' => '1'...
'3' => {'hanako' => {'name' => 'hanako','id' =>...
'2' => {'jiro' => {'name' => 'jiro','id' => '2'...
};
** fetchxxx_xxx系 [#d6acc21f]
prepare()とexecute()を実行した後に行う。
*** 1行を取得 [#qe945f62]
@row = $sth->fetchrow_array(); # 配列
$row = $sth->fetchrow_arrayref(); # 配列のリファ...
$row = $sth->fetch(); # 配列のリファ...
$row = $sth->fetchrow_hashref(); # ハッシュのリ...
$row = $sth->fetchrow_hashref('NAME_uc'); # 上とほぼ同じ...
*** 全行を取得 [#pe489866]
$rows = $sth->fetchall_arrayref(); ...
$rows = $sth->fetchall_arrayref([0]); ...
$rows = $sth->fetchall_arrayref([-2,-1]); ...
$rows = $sth->fetchall_arrayref({}); ...
$rows = $sth->fetchall_arrayref({id=>undef,name=>undef})...
** プレースホルダーに値を渡す [#r9e0243d]
selectxxx_xxx系でもfetchxxx_xxx系でも、引数の最後の方(( ...
$row = $dbh->selectrow_hashref( 'SELECT * FROM t1 WHERE ...
** 高速なSELECT [#v77e0889]
fetchrow_arrayrefとbind_columnsを使う。
$sth->bind_columns(\($num, $str));
while ($row = $sth->fetchrow_arrayref){
print "$num,$str\n";
}
** 影響した行数 [#jacd6b3d]
$sth->rowsはすべてfetchし終わった後しか使えない。よって、...
** 値のエスケープ [#xf9b9469]
$data = $dbh->quote("don't");
$sql = "SELECT * FROM tbl WHERE name = $data";
http://search.cpan.org/perldoc?DBI#quote
** 参考 [#f5e07016]
http://search.cpan.org/perldoc?DBI
ページ名: