用于LOAD DATA INFILE的perl DBI执行语句

时间:2013-10-14 00:37:26

标签: mysql perl dbi

我对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);

非常感谢所有帮助。

0 个答案:

没有答案