Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: How to execute sql scripts via perl

by samtregar (Abbot)
on Mar 04, 2009 at 05:55 UTC ( #747995=note: print w/replies, xml ) Need Help??

in reply to How to execute sql scripts via perl

It's possible, but not usually worth the effort. If you want to do it, what you need to do is read in the SQL file and split up each statement. Pass each statement to $dbh->do(). Something like (untested):

# 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: $?";


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
    Thanks for the reply.

    Now I am using the shell command. Its working

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://747995]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2022-05-22 01:59 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (78 votes). Check out past polls.