[英]DBD::CSV - How to get the column-names?
如何从表中获取列名? 我的尝试没有用:
#!/usr/bin/env perl
use warnings;
use 5.012;
use DBI;
my $options = { RaiseError => 1, PrintError => 0, f_ext => ".csv/r" };
my $dbh = DBI->connect( "dbi:CSV:", undef, undef, $options ) or die $DBI::errstr;
my $table = 'test';
$dbh->do( "CREATE TEMP TABLE $table ( id INT, size INT )" );
my $sth = $dbh->prepare( "INSERT INTO $table ( id, size ) VALUES( ?, ? )" );
$sth->execute( 1, 235 );
$sth->execute( 2, 42 );
use Data::Dumper;
say Dumper $dbh->{csv_tables}{$table}{col_names};
$dbh->{csv_tables}{$table} = { skip_first_row => 0 };
$sth = $dbh->prepare( "SELECT * FROM $table" );
$sth->execute;
my @first_row = $sth->fetchrow_array;
say "@first_row\n";
$sth = $dbh->column_info( '', '', $table, '' );
my $ref = $sth->fetchall_arrayref;
say "@$_" for @$ref;
# $VAR1 = undef;
#
# 1 235
#
# Can't call method "fetchall_arrayref" on an undefined value at ./so.pl line 25.
column_info
方法应该由驱动程序实现,如果尚未实现,则会得到undef
。
相反,在您执行第一个查询后,我将查看$sth->{NAME}
。
顺便说一句, DBD::CSV
是一个有趣的玩具,但是如果您需要轻便的一次性数据库,我强烈建议您改用DBD::SQLite
。 而且,如果您只需要处理CSV数据,则可以通过几个不错的模块来进行原始访问。 在这两者之间,剩下的用例很少,其中DBD::CSV
有意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.