JSchmitz has asked for the wisdom of the Perl Monks concerning the following question:

I am using the spreadsheet parse module and below is a sample
of the output from the script. Can anybody tell me how to create
and insert SQL statement out of this? It would be going into table
that is already created. The numbers in parens - the left one
is the file number and the right one is the column heading from
the spreadsheet - Thanks!!!
( 169 , 0 ) =>0000000000010173A001 ( 169 , 1 ) =>A ( 169 , 2 ) =>The Tigress ( 169 , 3 ) =>The Tigress ( 169 , 4 ) =>TIGRESS, THE (aka: Die Tigerin) ( 169 , 5 ) => ( 169 , 6 ) => ( 169 , 7 ) =>Yes ( 169 , 8 ) => ( 169 , 9 ) =>http://www.blockbuster.com/mv/detail.jhtml?PRODID=171427 +&CATID=1050 ( 169 , 10 ) =>Action ( 169 , 11 ) =>Karin Howard ( 169 , 12 ) =>Andreas Grothusen ( 169 , 13 ) =>H. Jaenicke ( 169 , 14 ) => ( 169 , 15 ) => ( 169 , 16 ) => ( 169 , 17 ) =>R ( 169 , 18 ) => ( 169 , 19 ) =>1992 ( 169 , 20 ) =>0000000000010173J001.jpg ( 169 , 21 ) => ( 169 , 22 ) =>Berlin, 1920s, a couple tries to blackmail an American +businessman, but nothing goes according to their plans. ( 169 , 23 ) => ( 169 , 24 ) =>In this tale of blackmail, passion and deception, Valen +tina Vargas, Georg Tryphon, James Remar and George Peppard star. In B +erlin in the 1920s, a couple lays a careful plan to blackmail an Amer +ican businessman. But the duo end up with a horrible mess on their ha +nds when no one behaves according to plan. ( 169 , 25 ) =>4.99 ( 169 , 26 ) =>24 ( 169 , 27 ) =>2000-12-01T00:00:01 ( 169 , 28 ) =>2002-10-15T23:59:59 ( 169 , 29 ) =>5 ( 169 , 30 ) => ( 169 , 31 ) =>0000000000010173A001 ( 169 , 32 ) => ( 169 , 33 ) => ( 169 , 34 ) => ( 169 , 35 ) => ( 169 , 36 ) =>CinemaNow ( 169 , 37 ) => ( 169 , 38 ) => ( 169 , 39 ) => ( 169 , 40 ) => ( 169 , 41 ) => ( 170 , 20 ) =>

Replies are listed 'Best First'.
Re: creating a SQL statement from an array
by suaveant (Parson) on Apr 16, 2001 at 21:34 UTC
    well... first you need an array of field names for the data... I will assume the second number ie the 36 in ( 169 , 36 ) is the column (field specifier) and the first number IE 169 is the row number, and that you put the field names in an array @field_names. With that I would do the following...
    my $oldrow; my(@fields,@values); while(<DATA>) { chomp; my ($row,$col,$data) = /^\s*\(\s*(\d+)\s*,\s*(\d+)\s*\)\s*=>(.*)$/; # that puts the row number in $1, col number in $2 and data in $3, a +nd in the variable respectively if($oldrow != $row) { if(@fields) { #make sure there is data $query = "INSERT INTO tablename(".(join ',', @fields)." VALUES(" +.(join ',', @values).')'; ... do DB calls ... @fields = @values = (); } $oldrow = $row; } push @fields, $field_names[$col]; # field name with proper data push @values, db->quote($data); #run data through DB quoter }
    so you go through, adding data to @fields and @values in order so they match up, then when you come to a new row number, you build your query (works with MySQL, think it will work in others) and execute it, then clear the fields and values and go onto next row. Hope that is clear enough
                    - Ant
      so if the original SQL statement is:

      SELECT junk.name, st.site_type, junk.location, junk.mapref
      FROM junksite junk, site_type st
      WHERE junk.site_type_id = st.id

      the arrays would map as follows

      name -> 0<br> site_type -> 1<br> location -> 2<br> mapref -> 3<br>
      so from there you can write
      $array_ref = $sth->fetchall_arrayref( [0, 2] );
Re: creating a SQL statement from an array
by michellem (Friar) on Apr 17, 2001 at 00:27 UTC
    I have an alternative approach - import comma (or tab) delimited text right into the database - save you from needing to use a spreadsheet parser. Here's the code:
    #!/usr/bin/perl -w # # Tool to import comma delimited files into a database # Works on command line # Usage: perl tableimport.pl # use DBI; use strict; print "Filename:"; #Name of Comma Delited file chomp (my $file = <>); print "DB:"; # Database name chomp (my $dbname = <>); print "Table:"; # Table name chomp (my $table = <>); open (FILE, "$file") or die "Couldn't open file!\n"; my @lines = <FILE>; #assume the column names are on the first line my $colnames = shift (@lines); chomp ($colnames); my @cols = split (',', $colnames); # do quotes separate the data? my $quotes = 0; if ($cols[0] =~ m/^\"[\S]*\"$/) { $quotes = 1; foreach (@cols) {s/\"//g;} } print "Column Names:\n"; foreach (@cols) {print; print "\n";}; print "Next Step?"; my $step = <>; my $num_cols = $#cols; my $dbh = DBI->connect("dbi:Pg:dbname=$dbname","","") or die "Can't co +nnect to database\n"; my $firstsql = "SELECT * FROM $table"; my $sth = $dbh->prepare("$firstsql") or die "Can't prepare SQL stateme +nt: $DBI::errstr\n"; $sth->execute or die "Can't execute: $DBI::errstr\n"; my @column; my $numfields = $sth->{NUM_OF_FIELDS}; for (my $i=0; $i<$numfields; $i++) { $column[$i] = $sth -> {NAME} -> [$i]; } $sth->finish; print "Columns in file:$num_cols\n"; print "Columns in database:$numfields\n"; my $dbcol; my %match = (); foreach (@cols) { foreach $dbcol (@column) { if ($_ eq $dbcol) {$match{$_} = 1}; } if (!$match{$_}) {print "$_ does not have a matching field nam +e - it will not be imported!\n";} } print "Next Step?"; $step = <>; my $j=0; foreach (@lines) { my $chip = chomp; print "Row:$j:, $chip\n"; print "Line:$_\n"; #Put the lines in a hash keyed to column names my %rows = (); my $i=0; my $col; my @data; if ($quotes) {s/"//g;} @data = split (','); foreach $col (@cols) { if ($match{$col}) { $rows{$col} = $data[$i]; print "$col: $rows{$col}\n"; } $i++; } print "\n"; # Build a SQL statement to insert each line from file $i=0; my $sql0 = "INSERT INTO $table ("; my @sqla = (); my @sqlb = (); my $key; foreach $key (keys %rows) { $sqla[$i] = $key; $sqlb[$i] = $dbh->quote($rows{$key}); $i++; } my $sql1 = join (',',@sqla); my $sql2 = ") VALUES ("; my $sql3 = join (',',@sqlb); my $sql4 = ")"; my $sql = "$sql0$sql1$sql2$sql3$sql4"; print "SQL:$sql\n"; print "Import? [Y/n]"; chomp(my $import = <>); if ($import !~ /n/i) { my $sth = $dbh->prepare("$sql") or die "Can't prepare +SQL statement: $DBI::errstr\n"; $sth->execute or die "Can't execute: $DBI::errstr\n"; print "Imported successfully\n"; } $j++; } $sth->finish; $dbh->disconnect;
    This is written for Postgres - could be re-written easily for any SQL that DBI handles.