DBD::CSV:文件扩展名问题
在此脚本中,我遇到文件扩展名问题: 如果我使用 /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 执行失败:
执行错误:没有从 /usr/local/lib/perl5/site_perl/5.12.0/x86_64-linux/DBD/File.pm 在 570 调用这样的列 '"/home/mm/test_1.csv".id'。 < /p>
输出没有文件扩展名:
“1”、“棕色”、“拉勒米”
“2”、“史密斯”、“沃特敦”
2行
这是一个错误吗?
猫测试_1.csv
ID、姓名
1、棕色
2、史密斯
5、绿色
猫test_2.csv
ID,城市
1、拉勒米
2、沃特敦
8、斯普林维尔
In this script I have problems with file-name-extensions:
if I use /home/mm/test_x it works, with file named /home/mm/test_x.csv it doesn't:
#!/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;
Output with file-name-extention:
DBD::CSV::st execute failed:
Execution ERROR: No such column '"/home/mm/test_1.csv".id' called from /usr/local/lib/perl5/site_perl/5.12.0/x86_64-linux/DBD/File.pm at 570.
Output without file-name-extension:
'1', 'Brown', 'Laramie'
'2', 'Smith', 'Watertown'
2 rows
Is this a bug?
cat test_1.csv
id,name
1,Brown
2,Smith
5,Green
cat test_2.csv
id,city
1,Laramie
2,Watertown
8,Springville
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DBD::CSV 提供了一种将查询中使用的表名映射到文件名的方法。相同的机制用于设置每个文件的属性,例如行结束符、字段分隔符等。在 DBD::CSV 文档中查找“csv_tables”。
就我而言,我必须指定行结束字符,因为我在 vi 中创建了 CSV 文件,因此它们最终以 Unix 行结束符结束,而 DBD::CSV 假定 DOS/Windows 行结束符,无论脚本运行在哪个平台上。
DBD::CSV provides a way to map the table names you use in your queries to filenames. The same mechanism is used to set up per-file attributes like line ending, field separator etc. look for 'csv_tables' in the DBD::CSV documentation.
In my case I had to specify a line ending character, because I created the CSV files in vi so they ended up with Unix line endings whereas DBD::CSV assumes DOS/Windows line-endings regardless of the platform the script is run on.
我看起来甚至这也有效:
I looks like even this works:
我想您可能想看看 f_ext 和 f_dir 属性。然后,您可以将表名称分类为“test_1”和“test_2”(不带 csv),但使用的文件将为 test_1.csv 和 test_2.csv。表名中的点的问题是点通常用于将模式与表名分开(请参阅 f_schema)。
I think you might want to take a look at the f_ext and f_dir attributes. You can then class your table names as "test_1" and "test_2" without the csv but the files used will be test_1.csv and test_2.csv. The problem with a dot in the table name is a dot is usually used for separating the schema from the table name (see f_schema).