Perl DBIのメソッド・プロパティ
PerlのDBIで、データベースハンドル、ステートメントハンドルから利用できるメソッドとプロパティのメモです。 とりあえず、一覧だけ。 あとで解説を付け足すかも。
データベースハンドル
メソッド
- do
SQLをいきなり実行。SELECTのように、結果行を返してほしい文には意味なし。 - selectrow_array
- selectall_arrayref
- selectcol_arrayref
- prepare
- prepare_chached
- commit
- rollback
- disconnect
- ping
- table_info※
- tables※
- type_info_all※
- type_info※
- quote
利用中のDBに応じたクオートを行う。
my $quoted_text = $dbh->quote("クオートしたい文字列、\とか'とか…");
プロパティ
- AutoCommit
- Driver
- Name
- RowCacheSize
[ ページ先頭へ ]
ステートメントハンドル
メソッド
- bind_param
- execute
- fetchrow_arrayref
- fetchrow_array
- fetchrow_hashref
- fetchall_arrayref
- finish
- rows
- bind_col
- bind_columuns
- dump_results
プロパティ
- NUM_OF_FIELDS
- NUM_OF_PARAMS
- NAME
- NAME_lc
- NAME_uc
- TYPE
- PRECISION
- SCALE
- NULLABLE
- CursorName
- Statement
- RowsInCache
[ ページ先頭へ ]
DBIメモ
DBIの使い方について、自分が使うためのメモです。 頻繁に忘れるんですよね...。
use DBI; use strict; my $dsn = 'DBI::msql:hostname:dbname'; # たとえばMySQL my $user = 'user'; my $pass = 'password'; my $dbh = DBI->connect( $dsn, $user, $pass, { RaiseError => 1, AutoCommit => 0 } ) or die("cannot connect: $DBI::errstr"); $dbh->do('何かSQL、updateとか') or die($dbh->errstr); $dbh->commit; # AutoCommit => 0 の場合 my $sth = $dbh->prepare('何かSQL、selectとか') or die ($dbh->errstr); $sth->execute() or die ( $sth->errstr ); while ( my @row = $sth->fetchrow_array ) { print join(",", @row), "\n"; } $sth->finish(); # 通常不要 $dbh->disconnect();
そのほかによく使うのは
if ($sth->rows) { while (my $ref = $sth->fetchrow_hashref) { print $ref->{'フィールド名'}; } } else { die('該当するデータが存在しませんでした。'); }
とか、そんな感じ。
最終更新日:2009/07/24
[ ページ先頭へ ]