in reply to How to execute sql scripts via perl
# read foo.sql into memory open my $fh, "<", "foo.sql" or die $!; my $sql = do { local $/; <$fh> }; # split on ; at the end of a line my @commands = split /;\s*\n/, $sql; # run each command $dbh->do($_) for @commands;
It's not perfect - it will break if you have a comment that ends in a ";" for example. Usually it's better to just shell out to the MySQL shell to load a whole SQL file:
system("mysql -hhost -uname -ppass db < foo.sql") == 0 or die "Failed: $?";
-sam
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to execute sql scripts via perl
by cnarun86 (Initiate) on Mar 04, 2009 at 07:40 UTC |