Front page | perl.dbi.users |
Postings from December 2006
Re: out of memory
Thread Previous
|
Thread Next
From:
Marius Feraru
Date:
December 28, 2006 19:08
Subject:
Re: out of memory
Message ID:
459486C0.7030908@n0i.net
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Ron Savage wrote:
> On Thu, 28 Dec 2006 13:48:28 -0800, louis fridkis wrote:
>> $printstring =~ s/,$/);/; print "$printstring\n"; }
> Using s// to replace a char is shooting a mouse with an elephant gun. Try:
> substr($x, length($x) - 1, 1) = ');';
You forgot to notice Louis' ugly way (and error prone) of determining
the right quoting (hint: $dbh->quote accepts two parameters) or the
horrid "optimization" of using "bind_columns" (destroyed afterwards by
populating "@rowvalues" in the loop).
Ugly, too ugly code for the new year's eve ;-)
Louis, as Ron already hinted, I'd skip debugging this code and start off
with some clean new snippet, free of all these feeble "optimizations" (i
suppose this is what you wanted - "optimizing").
Here is something for starters (take care, I'm just baking it, no
testing done):
# your prepare and execute
my $cel_sth = $dbh_from->prepare($sqlstring);
$cel_sth->execute();
### get/set info necessary to the INSERT generator
# get columns types
# ! sth attributes should help, read your DBD's docs for availability
# ! (else you'll have to fallback to using "table_info / column_info,
# ! yet another soft spot for some DBDs)
my $types = $cel_sth->{TYPE};
# prepare INSERT generator (previous issues apply here too)
my $insert_proto
= q{INSERT INTO }
. $dbh_from->quote_identifier($table_name)
. q{ (}
. join(
q{, },
map { $dbh_from->quote_identifier($_) } @{ $cel_sth->{NAME} }
)
. q{) VALUES (};
# loop through rows and print generated INSERT statements
while ( my $row = $cel_sth->fetchrow_arrayref ) {
my $col_idx = 0;
print
$insert_proto,
join(
q{, },
map { $dbh_from->quote( $_, $types->[ $col_idx++ ] ) } @{$row}
),
");\n";
}
Yes, lotta dbi-quoting... but remember that quoting may fail too (OK,
rarely, but it does - it's documented) ;-)
The only (apparently) tricky thing in fact is how to get that
"data_type" parameter used for properly quoting your generated
statements, but hopefully you use some DBD that supports at least
table_info / column_info (if sth attributes aren't available). I wrote
this snippet with DBD::mysql and DBD::Pg in mind.
cheers
- --
Marius Feraru
-----BEGIN PGP SIGNATURE-----
iD8DBQFFlIbAtZHp/AYZiNkRAjm/AKDuD+0jA48Eqh2KIpBB6chidyuK+wCgiD0s
iLl8SgRHW7bn6MwWcrHFm4k=
=YaLE
-----END PGP SIGNATURE-----
Thread Previous
|
Thread Next