LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-06-2011, 01:59 PM   #1
anon209
Member
 
Registered: Nov 2005
Location: North America
Distribution: Gentoo
Posts: 105

Rep: Reputation: 0
Using perl to update mysql table on certain event


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.

TIA
 
Old 02-07-2011, 05:56 AM   #2
goossen
Member
 
Registered: May 2006
Location: Bayern, Germany
Distribution: Many
Posts: 224

Rep: Reputation: 41
Can you post the error you are getting or what is the problem with the script you posted ?
 
Old 02-07-2011, 05:39 PM   #3
anon209
Member
 
Registered: Nov 2005
Location: North America
Distribution: Gentoo
Posts: 105

Original Poster
Rep: Reputation: 0
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.

Code:
Ray Charles;I Got A Woman;Golden Legends
Cheers
TIA

Last edited by anon209; 02-07-2011 at 07:52 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl: Want to compare CGI input with data from a MySQL table resetreset Programming 1 11-20-2008 06:58 AM
help with mysql command to update data in a table ncsuapex Programming 2 09-30-2008 11:32 AM
Way to update mysql table 200+ times at once macie Programming 7 04-13-2005 11:45 PM
mySQL table update DropHit Linux - Software 5 02-04-2004 10:47 PM
retriving mysql data, and placing it inside a table in html using perl rhuser Programming 1 03-12-2003 05:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration