in reply to How do I execute an SQL file through DBI
NUM_OF_FIELDS should reliably mark statements like SELECT that return fetchable data so this does a bit more than simply looping over $dbh->do().# ... $dbh->{RaiseError}=1; $dbh->{PrintError}=0; # ... for my $stmt( split /;\n+/, join('',<FH>) ){ my $sth = $dbh->prepare($stmt); $sth->execute; fetch_and_display($sth) if $sth->{NUM_OF_FIELDS} }
For more complex stuff I do things like use SQL comments to give names to the statements and/or include a trailing square-bracket list of bind values before the semicolon for placeholder statements
You can use DBI::Shell in a program rather than a shell (I've done it, it required a minor subclass IIRC). At the moment DBI::Shell AFAIK uses something very similar to what I've shown above so you won't really gain anything that way.-- get_customer_name SELECT name FROM customer WHERE id = ? [47];
|
|---|