ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'd like to use a perl script to update a fields in mysql database when pianobar (command line pandora radio player) starts a new song. Pianobar has the ability to run a script based on events, I'd like to take advantage of and use perl to parse the artist,song,album and update the corresponding fields. Then later retrieve them and display them with php/html. I am by no means proficient with perl. I started to use bash, but it looks like perl will be much more efficient and and less time consuming.
Code:
#! /usr/bin/perl -wT
use strict;
use DBI;
$ENV{PATH} = "/bin:/usr/bin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
if (my $action = shift @ARGV)
{
if ($action eq 'songstart')
my $dbh = DBI->connect('DBI:mysql:database=PandoraMusic;host=localhost','username','password')
or die "Cannot connect: " . $DBI::errstr;
$sql = qq`SELECT artist,song,album,time FROM pandora WHERE otherfield=1`;
$sth = $dbh->prepare($sql) or die "Cannot prepare: " . $dbh->errstr();
$sth->execute() or die "Cannot execute: " . $sth->errstr();
my ($artist,$song,$album,$time) = $sth->fetchrow_array();
$sth->finish();
print "Artist is $artist , Song is $song , Album is $album was played at $time\n";
if (@fields != 0) {
my $i=0;
foreach $line (@fields) {
print "row $i - a is @$line[0] , b is @$line[1] , c is @$line[2] \n";
$i++;
}
}
$sql = qq`UPDATE pandora SET field=1 WHERE otherfield=2`;
$sth = $dbh->prepare($sql) or die "Cannot prepare: " . $dbh->errstr();
$sth->execute() or die "Cannot execute: " . $sth->errstr();
$sth->finish();
$sql = qq`SELECT artist FROM table pandora name=username AND password=password`;
$sth = $dbh->prepare($sql) or die "Cannot prepare: " . $dbh->errstr();
$sth->execute($name, $password) or die "Cannot execute: " . $sth->errstr();
my $field = $sth->fetchrow_array();
$sth->finish();
I just want the perl script to update the feilds when the song changes. If anyone can help me out I'd really appreciate it.
use of uninitialized value in concatenation (.) or string at line 54. Can someone point me in the right direction?
Code:
#!/usr/bin/perl
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);
open (INFILE, "/tmp/nowplayingtest.csv") or die "Can't open file!";
while (<INFILE>)
{
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] $_->[3]\n\n";
}
}
An example of the CSV file is as follows. Contrary to to normal CSV files, this doesn't have a the field names on the first row.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.