Chinaunix

标题: 为何数据插入不到数据库? [打印本页]

作者: dahe_1984    时间: 2014-11-02 18:59
标题: 为何数据插入不到数据库?
#!/usr/bin/perl -w

use strict;  
use warnings;
use DBI;
use Encode;
use strict;
use Time::localtime;
use Data:umper;
use Time::Local;
use Spreadsheet:arseExcel;
use POSIX qw(strftime);

my $dbh = DBI->connect( 'DBI:mysql:altadb',
                        '',
                        '',
                        {RaiseError=>1,AutoCommit=>0}
                      )
          || die "Database connection not made: $DBI::errstr";

my $parser   = Spreadsheet:arseExcel->new();
my $workbook = $parser->parse('products.xls');

if ( !defined $workbook ) {
    die $parser->error(), ".\n";
}

for my $worksheet ( $workbook->worksheets() ) {

    my ( $row_min, $row_max ) = $worksheet->row_range();
    #my ( $col_min, $col_max ) = $worksheet->col_range();

        #3944
        my $count = 0;
    for my $row ( $row_min .. 18 ) {
           my $catalog = $worksheet->get_cell( $row, 0 )->unformatted();
           next unless $catalog;
                   if($catalog =~ /[^0-9a-zA-Z]/) {
                                next;
                   } else {
                                my $sth = $dbh->prepare("select t.catalog from kc_product t where t.catalog = '$catalog'";
                                $sth->execute();                           
                                if (my @ary = $sth->fetchrow_array()) {
                                        print "exitsary \n";
                                        next;
                                }
                                       
                                        #$count = $count +1;
                                        my $Cname = $worksheet->get_cell( $row, 1 )->unformatted();
                                        my $Pname = $worksheet->get_cell( $row, 2 )->unformatted();
                                        my $cas = ($worksheet->get_cell( $row, 3 )? $worksheet->get_cell( $row, 3 )->unformatted():'');
                                       
                                        my $sql = q{insert INTO kc_product (catalog, title, chemical_name, cas) VALUES (?, ?, ?, ?)};
                                        #$sth = $dbh->prepare("insert INTO kc_product (catalog, title, chemical_name, cas) VALUES (?, ?, ?, ?)";

                    print $count." ".$sql."\n" ;
                    $count = $count + 1;
                    $sth = $dbh->prepare($sql);
                    $sth->bind_param(1, $catalog);
                    $sth->bind_param(2, $Cname);
                    $sth->bind_param(3, $Pname);
                        $sth->bind_param(4,$cas);
                        $sth->execute();
                                       
                                        #my $sql = "INSERT INTO kc_product (catalog, title, chemical_name, cas) VALUES ('$catalog', '$Cname', '$Pname', '$cas')";
                                        #$dbh->do("$sql"||        die "Couldn't insert record : $DBI::errstr";
                                        print "insert $catalog,qq/$Cname/,$Pname,qq/$cas/\n";
                                       
                        }
        }               
}
$dbh->disconnect;



作者: dahe_1984    时间: 2014-11-02 19:00
程序没有报任何错误,但是区查看数据库,这两条数据没有插入进去,为何?
作者: dahe_1984    时间: 2014-11-02 19:09
都没人Le ,perl这么没前途了
作者: laputa73    时间: 2014-11-02 19:46
你忘了commit了。
前面设的autocomit=0





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2