免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3379 | 回复: 3
打印 上一主题 下一主题

为何数据插入不到数据库? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 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;


论坛徽章:
0
2 [报告]
发表于 2014-11-02 19:00 |只看该作者
程序没有报任何错误,但是区查看数据库,这两条数据没有插入进去,为何?

论坛徽章:
0
3 [报告]
发表于 2014-11-02 19:09 |只看该作者
都没人Le ,perl这么没前途了

论坛徽章:
42
19周年集字徽章-周
日期:2019-10-14 14:35:31平安夜徽章
日期:2015-12-26 00:06:30数据库技术版块每日发帖之星
日期:2015-12-01 06:20:002015亚冠之首尔
日期:2015-11-04 22:25:43IT运维版块每日发帖之星
日期:2015-08-17 06:20:00寅虎
日期:2014-06-04 16:25:27狮子座
日期:2014-05-12 11:00:00辰龙
日期:2013-12-20 17:07:19射手座
日期:2013-10-24 21:01:23CU十二周年纪念徽章
日期:2013-10-24 15:41:34IT运维版块每日发帖之星
日期:2016-01-27 06:20:0015-16赛季CBA联赛之新疆
日期:2016-06-07 14:10:01
4 [报告]
发表于 2014-11-02 19:46 |只看该作者
你忘了commit了。
前面设的autocomit=0
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP