發新話題

《Perl DBI 入門》選項

《Perl DBI 入門》選項

在連線資料庫時 connect() 這個方法可以接受 hash 型態的選項,常用的選項包括了: AutoCommit,設為 true 表示資料庫交易自動確認; RaiseError,告訴 DBI 在錯誤發生時觸發例外 croak $DBI::errstr 而不只是僅傳回錯誤代碼;PrintError,讓 DBI 以警告方式 warn $DBI::errstr 傳回錯誤訊息.

在下一段程式中,是希望進行採交易處理的用法,設定成 AutoCommit off,RaiseError on,而讓 PrintError 使用內定值 on.


use strict;
use DBI;

my $dbh = DBI->connect( 'dbiBMaker:dbsample',
                        'jerry',
                        'jerrypassword',
                        {
                          RaiseError => 1,
                          AutoCommit => 0
                        }
                      )
          || die "Database connection not made: $DBI::errstr";
$dbh->disconnect();
   
有一點要特別注意,如果資料庫本身不支援交易處理的功能時,設定 AutoCommit off 會接收到錯誤發生的傳回值.

TOP

發新話題

本站所有圖文均屬網友發表,僅代表作者的觀點與本站無關,如有侵權請通知版主會盡快刪除。