【问题标题】:DBD::CSV - How to get the column-names?DBD::CSV - 如何获取列名?
【发布时间】:2011-08-12 04:06:58
【问题描述】:

如何从表中获取列名?我的尝试没有奏效:

#!/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.

【问题讨论】:

    标签: perl csv dbi columnname


    【解决方案1】:

    column_info 方法应该由驱动程序实现,如果没有实现,您将得到undef

    我会在您执行第一个查询后查看$sth->{NAME}

    顺便说一句,DBD::CSV 是一个有趣的玩具,但如果您需要一个轻量级的一次性数据库,我强烈建议您改用DBD::SQLite。如果您只需要处理 CSV 数据,那么有几个不错的模块可以为您提供原始访问权限。在这两者之间,很少有 DBD::CSV 有意义的用例。

    【讨论】:

    • 谢谢,我已经用过 "$sth->{NAME},但我不记得了。我喜欢 csv 文件,因为很多程序和人类都可以读取它们。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-16
    • 1970-01-01
    • 2014-06-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    相关资源
    最近更新 更多