# untested but realistic
my $dbh = DBI->connect("...",{RaiseError => 1} ) or die "can't\n";
my $cust_sth = $dbh->prepare(
qq{SELECT cust_ID, cust_name from customer});
$cust_sth->execute();
while (my $cust = $cust_sth->fetchrow_hashref()) {
my $orders_sth = $dbh->prepare(
qq{select * from orders where cust_ID = ? });
# do something smart with customer name
$order_sth->execute($cust->{cust_ID});
while (my $order = $orders_sth->fetchrow_hashref()) {
# do something even smarter with the orders data
}
}
With DBIx::Simple, I should either copy the result of the first query to an array, and suffer the copy delay, or create two objects (and bear in mind that in a real application I need to use more than that), thus having two connections and their relative overhead.
I didn't want to talk about statement handles, but I get the impression that you think DBIx::Simple can use only one. However, there are result objects, that internally are just objects holding a sth., with an alternative, consistent interface.
I don't think existing scripts should be re-written, but here's yours for demonstration purposes:
use DBIx::Simple;
my $db = DBIx::Simple->connect("...",{RaiseError => 1} ) or die "can't
+\n";
my $cust_result = $db->query('SELECT cust_ID, cust_name FROM customer'
+);
while (my $cust = $cust_result->hash) {
my $orders_result = $db->query(
'SELECT * FROM orders WHERE cust_ID = ?',
$cust->{cust_ID}
);
while (my $order = $orders_result->hash) {
# code
}
}
Or, when slurping into memory is not a problem, you can write beautiful code with DBIx::Simple:
use DBIx::Simple;
my $db = DBIx::Simple->connect("...",{RaiseError => 1} ) or die "can't
+\n";
for my $cust ($db->query('SELECT cust_ID, cust_name FROM customer')->h
+ashes) {
for my $order ($db->query('SELECT * FROM orders WHERE cust_ID = ?'
+, $cust->{cust_ID}->hashes) {
# code
}
}
U28geW91IGNhbiBhbGwgcm90MTMgY
W5kIHBhY2soKS4gQnV0IGRvIHlvdS
ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
geW91IHNlZSBpdD8gIC0tIEp1ZXJk