1

我对 perl 和这个论坛比较陌生。我正在尝试使用 perl DBI for mySql LOAD DATA INFILE 语句将 csv 文件上传到 mySQL 数据库。但是,execute 语句返回一个 undef 值。但是,如果我使用 SELECT 或 DESC 语句,它工作正常。我怀疑查询中的单引号和大括号导致错误,但不知道如何调试。

   #!/usr/bin/perl -w
   use strict;
   use warnings;
   use File::Basename;

    use DBI;
    use DBD::mySQL;
    my $data_path="D:\\NickD\\Project\\StockData\\"; 
    my $db = "TestMMDB";
    my $user = "user";
    my $pass ="pass";
    my $host = "localhost";
    my $query ="";
    my @row;   

    my @files = glob("$data_path*.csv");
    DBI->trace(1);


    my $dbh = DBI->connect("dbi:mysql:$db:$host",$user,$pass);

    foreach my $file(@files){
        my $filename = basename($file);
        my ($db_table,$date) = split("_",$filename);       

        $query = q{LOAD DATA INFILE ? INTO TABLE ? FIELDS TERMINATED BY ',' (Date,Symbol,Open,High,Low,Close,Volume)};      

    my $sqlQuery = $dbh->prepare($query);
    my $rv = $sqlQuery->execute($file,$db.".".$db_table) or die "Oops!: Can't execute the query :".$sqlQuery->errstr;
    while (@row = $sqlQuery->fetchrow_array()) {
        print "@row\n";
        } 
    }
    my $rc = $dbh->disconnect();
    exit(0);

所有帮助将不胜感激。

4

0 回答 0