Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl DBD::CSV:文件扩展名有问题_Perl_Csv_Dbi_Filenames_Dbd - Fatal编程技术网

Perl DBD::CSV:文件扩展名有问题

Perl DBD::CSV:文件扩展名有问题,perl,csv,dbi,filenames,dbd,Perl,Csv,Dbi,Filenames,Dbd,在此脚本中,我遇到了文件扩展名问题: 如果我使用/home/mm/test_x,它可以工作,但如果文件名为/home/mm/test_x.csv,它就不能工作: #!/usr/bin/env perl use warnings; use strict; use 5.012; use DBI; my $table_1 = '/home/mm/test_1.csv'; my $table_2 = '/home/mm/test_2.csv'; #$table_1 = '/home/mm/test_1

在此脚本中,我遇到了文件扩展名问题: 如果我使用/home/mm/test_x,它可以工作,但如果文件名为/home/mm/test_x.csv,它就不能工作:

#!/usr/bin/env perl
use warnings; use strict;
use 5.012;
use DBI;

my $table_1 = '/home/mm/test_1.csv';
my $table_2 = '/home/mm/test_2.csv';
#$table_1 = '/home/mm/test_1';
#$table_2 = '/home/mm/test_2';

my $dbh = DBI->connect( "DBI:CSV:" );
$dbh->{RaiseError} = 1;

$table_1 = $dbh->quote_identifier( $table_1 );
$table_2 = $dbh->quote_identifier( $table_2 );

my $sth = $dbh->prepare( "SELECT a.id, a.name, b.city FROM $table_1 AS a NATURAL JOIN $table_2 AS b" );

$sth->execute;
$sth->dump_results;
$dbh->disconnect;
文件扩展名为的输出:

DBD::CSV::st执行失败:
执行错误:在570处没有从/usr/local/lib/perl5/site\u perl/5.12.0/x86\u 64-linux/DBD/File.pm调用此类列'/home/mm/test\u 1.csv'.id'

不带文件扩展名的输出:

“1”、“棕色”、“拉勒米”
“2”、“史密斯”、“水城”
2排

这是虫子吗

cat测试_1.csv

id、名称
1、棕色
2,史密斯
5、绿色

cat测试_2.csv

id,城市
1,拉拉米
2、水城
斯普林维尔8号


CSV提供了一种将查询中使用的表名映射到文件名的方法。同样的机制用于设置每个文件的属性,如行尾、字段分隔符等。请在DBD::csv文档中查找“csv_表”

#!/usr/bin/env perl

use warnings;
use strict;

use DBI;

my $dbh = DBI->connect("DBI:CSV:f_dir=/home/mm", { RaiseError => 1 });
$dbh->{csv_tables}->{table_1} = {
    'file' => 'test_1.csv',
    'eol' => "\n",
};
$dbh->{csv_tables}->{table_2} = {
    'file' => 'test_2.csv',
    'eol' => "\n",
};

my $sth = $dbh->prepare( "SELECT a.id, a.name, b.city FROM table_1 AS a NATURAL JOIN table_2 AS b" );

$sth->execute();
$sth->dump_results();
$dbh->disconnect();

在我的例子中,我必须指定一个行尾字符,因为我在vi中创建了CSV文件,因此它们以Unix行尾结束,而DBD::CSV假定DOS/Windows行尾,而不管脚本在哪个平台上运行。

我看起来甚至可以这样做:

#!/usr/bin/env perl
use warnings; use strict;
use 5.012;
use DBI;

my $dbh = DBI->connect("DBI:CSV:f_dir=/home/mm/Dokumente", undef, undef, { RaiseError => 1, });

my $table = 'new.csv';
$dbh->do( "DROP TABLE IF EXISTS $table" );
$dbh->do( "CREATE TABLE $table ( id INT, name CHAR(64), city CHAR(64) )" );
my $sth_new = $dbh->prepare( "INSERT INTO $table( id, name, city ) VALUES ( ?, ?, ? )" );

$dbh->{csv_tables}->{table_1} = { 'file' => '/tmp/test_1.csv', 'eol' => "\n", };
$dbh->{csv_tables}->{table_2} = { 'file' => '/tmp/test_2.csv', 'eol' => "\n", };
my $sth_old = $dbh->prepare( "SELECT a.id, a.name, b.city FROM table_1 AS a NATURAL JOIN table_2 AS b" );
$sth_old->execute();

while ( my $hash_ref = $sth_old->fetchrow_hashref() ) {
    state $count = 1;
    $sth_new->execute( $count++, $hash_ref->{'a.name'}, $hash_ref->{'b.city'} );
}
$dbh->disconnect();

我想您可能想看看f_ext和f_dir属性。然后,您可以将表名分类为“test_1”和“test_2”,而不使用csv,但使用的文件将是test_1.csv和test_2.csv。表名中的点的问题是,点通常用于将模式与表名分开(请参见f_schema)。

您还可以在csv文件中提供示例数据吗。