I'm trying to do a batch insert of rows of data into a postgres database.
I have the data populated in an array ref, I think. It was fetched using a perl script from a space delimited file using the Spreadsheet::BasicReadNamedColmodule. The code to fetch the data is
$ss = new Spreadsheet::BasicReadNamedCol($xlsFileName) || die "Could not open '$xlsFileName': $!"; $ss->setColumns(@columnHeadings); my @array; my $row = 0; while (my $data = $ss->getNextRow()) { $row++; push @array, "@$data"; } Below is the content of the array ref.
Cristan McX 123 W State Street North Aurora IL William Sch 123 South Third St #367 Geneva IL Kellie xxx 123 South East St. Gardner IL John xx 321 Princeton Ct. Frankfort IL Peter xxxxxxx 123 N Myrtle Avenue Elmhurst IL Izabella xxx 321 S 3rd St. #367 Geneva IL The Perl DBI code I'm using to do the inserts is:
my $dbh = DBI->connect("DBI:Pg:dbname=greenthumb;host=localhost; port=5432","","", {'RaiseError' => 1}); my $sth = $dbh->prepare( 'INSERT INTO testtable (?, ?, ?, ?, ?, ?)' ); foreach(@array) { $sth->execute( @{$_} ); } $sth->finish; The error I'm getting is:
Can't use string ("FirstName LastName BusinessName "...) as an ARRAY ref while "strict refs" in use at ./f.pl line 38.
$_is not an array, it is a string. You'll likely want tosplitit first.123 W State Street North?VALUESbefore your list of values:INSERT INTO table_name VALUES .... It seems like you probably want to do your inserts in the loop that iterates over the spreadsheet contents rather than stringifying and de-stringifying.$_is not an arrayref, it is a string. But$datawas an arrayref. You could have just saidpush @array, $data.