Hi Monks, I am relatively new to perl and this forum. I am trying to use perl DBI for mySql LOAD DATA INFILE statement to upload a csv file to a mySQL database. However, the execute statement returns an undef value. However if i use a select or desc statement, it works fine.

#!/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; ## First get all files to upload, scan names, identify table to be upl +oaded in my @files = glob("$data_path*.csv"); DBI->trace(1); ## Connect to the database my $dbh = DBI->connect("dbi:mysql:$db:$host",$user,$pass); ## Gather the files to upload to DB foreach my $file(@files){ my $filename = basename($file); my ($db_table,$date) = split("_",$filename); ## Create Query # $query ="SELECT * FROM $db.$db_table"; $query = q{LOAD DATA INFILE ? INTO TABLE ? FIELDS TERMINATED B +Y ',' (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);

All help will be greatly appreciated.


In reply to perl DBI LOAD DATA INFILE by nickd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.