【问题标题】:"Can't find table names in FROM clause" error with DBD::CSVDBD::CSV 出现“在 FROM 子句中找不到表名”错误
【发布时间】:2014-01-27 22:20:47
【问题描述】:

我正在尝试使用DBI 中的UNION 语句来合并两个CSV 文件:

#!/usr/bin/perl -w

use strict;
use DBI;

my $dbh = DBI->connect ("dbi:CSV:") 
    or die "Cannot connect to the CSV file: $DBI::errstr()";
$dbh->{RaiseError} = 1;
$dbh->{TraceLevel} = 0;

my $query = "select * from file.csv UNION select * from output.csv";
my $sth = $dbh->prepare ($query);
$sth->execute ();
$sth->dump_results();
$sth->finish();
$dbh->disconnect();

但是,我收到以下错误:

在 FROM 子句中找不到表名!在 C:/Perl64/site/lib/SQL/Statement.pm 第 88 行。
DBD::CSV::db 准备 失败:在 FROM 子句中找不到表名!在 C:/Perl64 /site/lib/SQL/Statement.pm 第 88 行。
[for Statement "select * from file.csv UNION select * from output.csv"] at CSV.pl 第 15 行。
DBD::CSV::db 准备失败:在 FROM 子句中找不到表名!在 C:/Perl64 /site/lib/SQL/Statement.pm 第 88 行。
[for Statement "select * from file.csv UNION select * from output.csv"] at CSV.pl line 15.

我按照其他地方的建议更新了SQL::StatementSQL::Parse,但这并没有解决问题。我在 Windows 8.1 上运行。是什么导致了错误?

【问题讨论】:

    标签: perl csv dbi


    【解决方案1】:

    从查询中删除 .csv 扩展名并确保您的文件在当前目录中:

    my $query = "select * from file UNION select * from output";
    

    您也可以使用 csv 文件显式设置文件夹,

    my $dbh = DBI->connect ("dbi:CSV:", "", "", {
        f_dir => 'C:\path_to_csv',
    });
    

    【讨论】:

      【解决方案2】:

      DBD::CSV 使用SQL::Statement 作为其 SQL 引擎。 SQL::Statement only supports a subset of SQL commands,其中不包括UNION

      作为替代方案,为什么不简单地连接两个文件?

      【讨论】:

        猜你喜欢
        • 2019-11-24
        • 2021-11-09
        • 2022-11-08
        • 2018-05-23
        • 1970-01-01
        • 2021-07-21
        • 2019-04-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多