Perl读取xls插入到mysql

时间:2016-05-10 07:12:03

标签: mysql excel perl

我是Perl的新手,我正在编写一个Perl脚本来从xls读取数据并将结果插入MySQL DB,但我有问题... 这是我的代码:

#!/usr/local/bin/perl
use strict;
use warnings;
use diagnostics;
use Spreadsheet::ParseExcel;
use DBI;
use Data::Dumper qw(Dumper);
my $parser   = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse('test.xls');
my $dbh = DBI->connect("dbi:mysql:parser", "root", "123qwe", { RaiseError => 1}) or die $DBI::errstr;
my $query = 'INSERT INTO parser (Name,Country) VALUES (?,?)';
my $sth = $dbh->prepare($query) or die "Prepare failed: " . $dbh->errstr();

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();

        for my $row ( $row_min .. $row_max ) {
                for my $col ( $col_min .. $col_max ) {

                        my $cell = $worksheet->get_cell( $row, $col );
                        next unless $cell;
                        my $results = $cell->value();
                        open(my $fh, '>>', "test");
                        print $fh "$results\t";
                        close $fh;
                }
        }
}

open my $fh, "<", "test" or die $!;
while (<$fh>)
{
        chomp;
        my @vals = split;
        $sth->execute(@vals);
}
close $fh;

因此,当我执行脚本时,它以以下错误结束:

DBD::mysql::st execute failed: called with 6 bind variables when 2 are needed at ./parser.pl line 39, <$fh> line 1.
Uncaught exception from user code:
    DBD::mysql::st execute failed: called with 6 bind variables when 2 are needed at ./parser.pl line 39, <$fh> line 1.

这很自然,因为我在输出中确实有6个变量:

John Smith  USA Ognyan Penkov   Egypt

所以问题是我似乎无法找到一种方法来分割每个列/行的结果并将它们放在MySQL表中,因为Spreadsheet :: ParseExcel将所有日期都读为1行。(例如名称必须去表名和国家到表国家)

我的XLS文件如下所示:

       A           B
1. John Smith     USA
2. Ognyan Penkov  Egypt
...etc...

我的MySQL表:

+--------+--------------+------+-----+---------+----------------+
| Field  | Type         | Null | Key | Default | Extra          |
+--------+--------------+------+-----+---------+----------------+
| id     | int(6)       | NO   | PRI | NULL    | auto_increment |
| Name   | varchar(255) | YES  |     | NULL    |                |
|Country | varchar(255) | YES  |     | NULL    |                |
+--------+--------------+------+-----+---------+----------------+

1 个答案:

答案 0 :(得分:1)

您正在打开一个名为test的文件,用于为文档中的每个单元附加写。然后添加当前单元格的值,后跟制表符\t字符。之后你逐行打开并读取同一个文件(但只有一行),chomp关闭一行不结束的行(因为你没有放一行)和split在空格上,because split without a delimiter uses \s单个空格

  

如果省略,PATTERN默认为单个空格“”,触发前面描述的awk仿真。

这正是问题,因为你的文件看起来像这样:

John\tSmith\tUSA\tOgnyan\tPenkov\tEgypt

\t单个空格,所以最终@val就是所有这些。如果您将其传递给查询,则会失败。

正如您所说的那样,您没有表格和国家/地区的表格,但您所做的只是将Excel文件中的数据逐行放入单个MySQL表 ,您可以在$row循环中执行此操作。

for my $worksheet ( $workbook->worksheets() ) {
    my ( $row_min, $row_max ) = $worksheet->row_range();
    my ( $col_min, $col_max ) = $worksheet->col_range();

    for my $row ( $row_min .. $row_max ) {
        my @values;
        for my $col ( $col_min .. $col_max ) {
            my $cell = $worksheet->get_cell( $row, $col );
            next unless $cell;
            push @values, $cell->value();
        }
        $sth->execute(@values) or die $dbh->errstr;
    }
}