#!/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 () { 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 ); } }