Perl: How Do I Enclose Variable in SQL Query?

Iolaire

Registered
Hello, I'm developing my first perl script to scrape some data from the internet and put it into a MySQL database with perl. The page scrapping is working now but I can not get the values of variables inserted correctly.

DBD::mysql is not installing one my system so I'm using Net::MySQL. I can not get the query string to convert my variables into the correct values. Instead I'm getting the variables names inserted into MySQL - 1222, $locName[4], $dateName[4], $messageBody[4].

Here is my test code for the database portion:
Code:
use Net::MySQL;
my $mysql = Net::MySQL->new(
	hostname => 'localhost',   
	database => 'myDatabase',
	user     => 'XXX',
	password => 'XXX'
);
# here I set up test value to go into database
$locName[4] = "MYname";
$dateName[4] = "12/08/03 02:15 pm";
$messageBody[4] = "Some long String of Text including all kinds stuff.";
$replyName[4] = "1232";

    $mysql->query(q{
	INSERT INTO myTable VALUES ('1224',"$locName[4]", '$dateName[4]',"$messageBody[4]",'$replyName[4]')
  });

  printf "Affected row: %d\n", $mysql->get_affected_rows_length;
  $mysql->close;
I've tried using " instead of ' and putting "" around the entire Insert statement. However I have not gotten that to work. But I might be missing something.

Please could someone explain how to correctly enclose the valuable names so their values are inserted?
Thanks,
iolaire
 
Try using qq instead of q:

Code:
$mysql->query(qq{
	INSERT INTO myTable VALUES ('1224',"$locName[4]", '$dateName[4]',"$messageBody[4]",'$replyName[4]')
});
 
Yes that is correct. To interpolate variables in a string you need to use "$variable" instead of '$variable'.

Rgrds
Dan
 
Back
Top