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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.