in reply to Replacing a ' in a string
Also, I'd recommend not using temporary variables inside your while loop for all the data. With placeholders, you can just pass the entire @data array as arguments for the SQL statement, and also reuse the SQL statement for each insert statement. Here's an example of how you can avoid using the temporary variables (untested):
Notice how much easier this code would be to update if your flatfile format were changed, as merlyn suggests above. I think once you get the hang of placeholders, you will really be glad you did -- they make inserting data a breeze (among their other good qualities). If you have any questions about these suggestions, feel free to ask. Also, I'd recommend reading the DBI documentation sections about placeholders, and using Super Search here at the monestary to learn more.my @fields = qw/CLIENT RECORD CONTROL CUSIP .../; ... # open file and run sanity checks my $columns = join ',', @fields; # create the correct number of placeholders my $placeholders = join ',', ('?') x @fields; my $sql = "INSERT INTO table ($columns) values ($placeholders)"; my $sth = $dbh->prepare($sql); while ( defined $line = <INFILE> ) { chomp $line; my @data = split /\t/, $line; ... # probably should check to see if all the fields are defined. # then insert using our statement, passing the values of @data to # replace the placeholders in the SQL statement $sth->execute(@data); }
Hope this helped.
blokhead
|
|---|