Databases Reference
In-Depth Information
print "===========\n",
"Total:\t$Total\n";
The values are assigned to variables in the bind_columns( ) function in the order they
appear in the results of the SELECT , so we fetch the query results with the
fetchrow_arrayref( ) function, rather than fetchrow_hashref( ) . While
fetchrow_hashref( ) is often more convenient in other circumstances, there is no ad-
vantage to it here, and it would run more slowly. Note that when passing the variable
names to the bind_columns( ) function, we add a backslash symbol ( \ ) in front of the
dollar symbol so that Perl leaves the variable name intact and doesn't replace it with
the value of the variable.
The Complete Script Using Both Types of Binding
For our example of loading data into the database, we can put both parts together to
write a single script that loads data into the database and accesses this data, using both
types of binding, as shown in Example 17-5.
Example 17-5. Perl script with both types of binding
#!/usr/bin/perl
use DBI;
use strict;
# If the user hasn't provided any command-line arguments, provide a
# helpful error message.
if(@ARGV!=1)
{
die("Syntax: $0 [Input file]\n");
}
# If the user has provided the command line arguments, fill in the
# Animals hash with the corresponding values.
# Open the file specified on the command line; if we can't open it,
# print an error message and stop.
open(INPUTFILE, $ARGV[0])
or die("Failed opening $ARGV[0]\n");
####################################################################
# Load data from the CSV file into the Animals hash
my %Animals;
while(<INPUTFILE>)
{
# Remove the newline at the end of the line
chomp;
# Split the line by commas
my @AnimalsData=split(",", $_);
# Assign the text before the first comma to the name
my $AnimalName=@AnimalsData[0];
 
Search WWH ::




Custom Search