# The first column in every line is ignored, it is not obvious why in this example but in the original it was necessary. When using MySQL 4.1 make sure the password for MySQL has been set to old password otherwise connect will fail. Feel free to change the username/password. I have also included the code that uses the SELECT count(*)...INSERT...UPDATE sequence that works on 4.1 but doesn't consume large amounts of memory. If you wish to see proof comment the INSERT...On DUPLICATE UPDATE lines out and uncomment the SELECT count(*)...INSERT...UPDATE lines. # The usual use use DBD::mysql; # Connect to the database my ($dbh, $sth, $count); $dbh = DBI->connect ("DBI:mysql:host=localhost;database=testing", "nobody", "nobody", {PrintError => 0, RaiseError => 1}); open(B, "bigfile.txt") or die "no bigfile\n"; # Read in the columns $Columns = ; chomp($Columns); # Count the number of columns in the file $TempLine = $Columns; $col = ($TempLine =~ s/","//g); $cols = $col + 1; $Columns =~ s/\'/\`/g; $Columns =~ s/\"/\'/g; # Split the columns up @Colu = split(/','/, $Columns); chop($Colu[$col]); #Read in the lines while() { $Line = $_; chomp($Line); # Clean up the lines $Line =~ s/\'/\`/g; $Line =~ s/\"/\'/g; $Line =~ s/\r//g; $Line =~ s/\n//g; # Split the line up and remove trailing ' symbol @Liner = split(/\',\'/, $Line); chop($Liner[$col]); # Setup the columns and values for inserting into mysql for($z=1; $z < $cols; $z++) { if($z==1) { $ColumnSyntax = "$Colu[1]\,"; $LineSyntax = "\'$Liner[1]\'\,"; $NewLiner = "$Colu[1]\ \= \ \'$Liner[1]\'\,"} else { $Lengthy = 0; $Lengthy = length($Liner[$z]); if($Lengthy > 0) { $ColumnSyntax = "$ColumnSyntax $Colu[$z]\,"; $LineSyntax = "$LineSyntax \'$Liner[$z]\'\,"; $NewLiner = "$NewLiner $Colu[$z]\ \= \ \'$Liner[$z]\'\,"; }}} chop($ColumnSyntax); chop($LineSyntax); chop($NewLiner); $Result = "Column3 = \'$Liner[2]\'"; # Setup the shiznit for 4.1 $query = "Insert INTO TestMe ($ColumnSyntax) VALUES($LineSyntax) ON DUPLICATE KEY UPDATE $NewLiner"; $sth = $dbh->prepare ($query); $sth->execute (); $sth->finish (); #$query1 = "SELECT count(*) from TestMe where $Result"; #$sth = $dbh->prepare ($query1); #$query1 =~ s/\r//g; #$sth->execute (); #while(my @val = $sth->fetchrow_array()) { #$TheCount = $val[0] } #$sth->finish (); #if($TheCount == 0) { #$query2 = "Insert INTO TestMe ($ColumnSyntax) VALUES($LineSyntax)"; #$sth = $dbh->prepare ($query2); #$query2 =~ s/\r//g; #$sth->execute (); #$sth->finish ();} #else { #$query3 = "UPDATE TestMe SET $NewLiner where $Result"; #$sth = $dbh->prepare ($query3); #$query3 =~ s/\r//g; #$sth->execute (); #$sth->finish (); } } close(B); $dbh->disconnect ();