.... For example, the author is either unaware of hash slices
or chooses not to use them in the first code example. While
constructing SQL statements with many fields and placeholders
is tedious, and the Insert() function of DBIx::Recordset is
shorter, one could reduce 15 or more lines of repetitive code
[in the DBI example] with hash slices .... It would be possible
to improve the final example by using placeholders,
as well ....
####
$sql='insert into uregisternew
(country, firstname, lastname, userid, password, address1, city,
state, province, zippostal, email, phone, favorites, remaddr,
gender, income, dob, occupation, age)
values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
my @data=($formdata{country}, $formdata{firstname}, $formdata{lastname},
$formdata{email}, $formdata{password}, $formdata{address},
$formdata{city}, $formdata{state}, $formdata{province},
$formdata{zippostal}, $formdata{email}, $formdata{phone},
$formdata{favorites}, $formdata{remaddr}, $formdata{gender},
$formdata{income}, $formdata{date}, $formdata{occupation},
$formdata{age});
$sth2 = $dbh->prepare($sql);
$sth2->execute(@data);
$sth2->finish();
##
##
$sth2->execute(@data);
to
$sth2->execute(@formdata{@formdata_column_ordering});
##
##
sub make_placeholders {
sprintf "(%s)", join ',', '?' x @_;
}
sub make_columns {
sprintf "(%s)", join ',', @_;
}
##
##
$sql = sprintf "insert into %s values %s",
make_columns(@formdata_column_ordering),
make_placeholders(@formdata_column_ordering) ;
##
##
The key way to determine whether a particular module/library is
matched to the level of a task is to count the number of lines of
``prep code'' you must write before you can do what you want.
##
##
1 create 2 subroutines
2 use these subroutines to generate sql
3 connect to the database
4 obtain database and statement handles
5 commit the record to database.
##
##
DBIx::Recordset->Insert({ %connection_hash, %formdata });
##
##
In most cases the gap between DBI's API and Perl applications
has been bridged by indiscriminately mixing generic
application-level functionality with the specifics of the
current application.
##
##
$sql= sprintf 'insert into uregisternew
(%s) values (%s)',
join ',', @formdata_ordering,
join ',', '?' x @formdata_ordering;
##
##
Another maladaptive way that the DBI API has been extended for
application-level databasing is by developing a collection of
generic application-level tools but not publishing them.
##
##
The final way to misuse DBI in an application is to use it
directly.
##
##
.... For example, the author is either unaware of hash slices
or chooses not to use them in the first code example.
##
##
It would be possible to improve the final example by using
placeholders, as well ....