cmdlinegeek
IS-IT--Management
Not receiving any errors after compiling, but the script isn't reading the CSV file into the MySQL table. Can someone point me in the right direction? Many thanks.
Code:
#!/usr/bin/perl -w
use DBI;
use strict;
use warnings;
# Declare varaibles
my $DBNAME = "Test";
my $DBTABLE = "Pandora";
my $DBUSER = "slacker";
my $DBPASS = "password";
my $DBHOST = "localhost";
my $csvfile = "/tmp/nowplayingtest.csv";
# Connect to database at hand, or die
my $dbh = DBI->connect("DBI:mysql:$DBNAME:$DBHOST", $DBUSER, $DBPASS)
or die "Cannot connect to the database";
open (INFILE, $csvfile)
or die "Can't open file!";
while (<INFILE>)
{
chomp;
my @rows = split(';', $_);
my $artist = $rows[0];
my $title = $rows[1];
my $album = $rows[2];
$dbh->do(qq/insert into "$DBTABLE" (Artist, Title, Album) values ($artist, $title, $album/) or warn "failed to insert $artist, $title, $album into table - $dbh->errstr" if ($@);
my $res = $dbh->selectall_arrayref( q( SELECT Artist, Title, Album FROM Pandora));
foreach( @$res )
{
print "\n$_->[0], $_->[1], $_->[2]\n\n";
}
}
$dbh->disconnect;