in reply to array as mysql db rows
#!/usr/bin/perl use strict; use DBI; my $tablename = 'table_name'; # maybe this is really a file name? my $last_col = 20; # pick a number (any number?) -- you must know the +right one my $colnames = join( ',', map { sprintf( "field%d", $_ ) } 1 .. $last_ +col ); my $placehld = join( ',', map { '?' } 1 .. $last_col ); my $sql = "insert into $tablename ($colnames) values ($placehld)"; print STDERR "Ready to do:\n $sql\n"; # see how that looks open( DATAFILE, "wheres_the_data" ) or die "wheres_the_data: $!"; my $dbh = DBI->new( blah blah ); my $insert_data = $dbh->prepare( $sql ); foreach (<DATAFILE>) { chomp; my (@ROW) = split /,/; if ( @ROW != $last_col ) { # what do you do if the number of "fields" in ROW # is greater than or less than the number of columns # in the table? Think about that, and do it here. } else { $insert_data->execute( @ROW ); } }
So there were a lot of problems with the code you posted, like using "print" where you needed to use "sprintf", not doing "chomp" on the input data file, and really misinterpreting what the DBI man page tells you about how to use placeholders.
I'm hoping that if you start with this version, you'll get what you want more easily.
|
|---|