perl dbi 控制事务

<pre name="code" class="sql">jrhxpt01:/root# cat dbi.pl 
use DBI;  
 
#!/usr/bin/perl 
$db_name='test';
$ip='127.0.0.1';
$user="root";
$passwd="a2p13mvh";
$dbh="";
$dbh = DBI->connect("dbi:mysql:database=$db_name;host=$ip;port=3306",$user,$passwd, {
                          RaiseError => 1,
                          AutoCommit => 0
                        }) or die "can't connect to database ". DBI-errstr; 
##Connect  
eval{
    $dbh->do("insert into t1 values(2)");   
    print "111111111\n";
    $dbh->do("insert into t1 values(3)");   
    print "22222222\n";
    $dbh->do("insert into t1 values(4)");  
    print "33333333\n";
    $dbh->do("insert into t1 values(5)"); 
    print "444444444\n";
    $dbh->do("insert into t1 values(6)"); 
    print "555555555\n";
    $dbh->do("insert into t1 values(6)"); 
    print "66666666\n";
    #$dbh->do("insert into t1 values(1)");   
$dbh->commit();
     };
if( $@ ) {
    warn "Database error: $DBI::errstr\n";
    $dbh->rollback(); #just die if rollback is failing
} 
jrhxpt01:/root# perl dbi.pl 
111111111
22222222
33333333
444444444
555555555
DBD::mysql::db do failed: Duplicate entry '6' for key 't1_idx1' at dbi.pl line 25.
Database error: Duplicate entry '6' for key 't1_idx1
~                                                                                                                                                                                                         
~ 启用RaiseError 错误处理属性,以便这些错误用一条错误消息就能自动地终止相应的脚本。



            
阅读更多
个人分类: Perl DBI
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭