【发布时间】:2014-02-09 16:33:32
【问题描述】:
我在 Perl 中使用 DBI::CSV 模块来解析我的 csv 并对数据运行查询。
我的数据是这样的
1001|23|1|loading
1012|25||loading
我希望第二行中的第三个字段是 undef,这是我无法实现的。我将字段作为空字符串而不是 undef,这是我尝试过的一段代码。
use strict;
use warnings "all";
use Text::CSV_XS;
use DBI;
my $dbh = DBI->connect( "dbi:CSV:", undef, undef, {
csv_sep_char => "|",
f_dir => ".",
csv_eol => "\n",
csv_empty_is_undef => 1,
csv_blank_is_undef => 1,
csv_quote_char => undef,
csv_escape_char => undef,
csv_always_quote => undef,
f_ext => ".csv",
f_enc => "utf-8",
csv_class => "Text::CSV_XS",
RaiseError => 1,
PrintError => 1
}
);
my @cols = ("col1", "col2", "col3", "col4");
$dbh->{'csv_tables'}{'info'} = { 'file' => "file.csv", col_names => \@cols };
my $result =$dbh->selectall_hashref( "select col1,col2,col3,col4 from info where col1 = 1012", "col1")
#gives the following result
0 HASH(0x992573c)
1012 => HASH(0x9900e90)
'col1' => 1012
'col2' => '25'
'col3' => ''
'col4' => 'loading'
我在这里期望 col3 的值为 undef。
我将不胜感激这里的任何帮助。谢谢
【问题讨论】:
标签: perl csv perl-module dbi