[luau] Perl Frustration!!
knowtree at aloha.com
knowtree at aloha.com
Tue Aug 19 11:31:01 PDT 2003
On Sun, 17 Aug 2003 16:42:51 -1000
Deven Phillips <dphillips at gothic-hawaii.com> wrote:
> Blech!!! Perl DBI is frustrating in and of itself... The C API is much cleaner!!!
I disagree. I don't know about the C API, but the Perl DBI module makes perfect
sense to me.
Here is a piece of an equipment database I wrote:
# simple query to get cpu s/n and ip for identification
my $sql = "select wkstncpu.sn, ipaddr from wkstncpu
where wkstncpu.code = $systemcode;";
my $sth = $dbh->prepare($sql)
or die "Cannot prepare: " . $dbh->errstr();
# start the query
$sth->execute() or die "Cannot execute: " . $sth->errstr();
# fetch result and assign
@row = $sth->fetchrow_array();
$cpusn = $row[0];
$ip = $row[1];
$sth->finish;
There are always five steps. 1) Stuff a SQL statement into a scalar (here
$sql). 2) Prepare (precompile) the SQL. Not always reqired by the underlying
database, but leave it in so your code doesn't break elsewhere. 3) Execute the
SQL statement; this will create a result stream. 4) Read the result stream.
There are several choices, here I use the fetchrow_array method to parse a
record and place the pieces into succesive elements of the array @row.
5) Finish.
Often the result set will contain multiple records. Here is an example from a
different part of the program, showing a loop building HTML listbox elements:
# cycle through result one row at a time
# and fill listbox OPTION tags
while (@row = $sth->fetchrow_array()) {
print "<OPTION>" . join(",", @row) . "</OPTION>";
}
The only thing I grumble about is the way assigning $row[n] to a variable
doesn't support built-in position-independent data element names. A schema
change can trigger a complete application re-write. Instead of
@row = $sth->fetchrow_array();
$cpusn = $row[0];
$ip = $row[1];
I would like to be able to write
$cpusn = $sth->fetchitembyname("wkstncpu.sn");
$ip = $sth->fetchitembyname("wkstncpu.ipaddr");
and have the location of the data in the record resolved by means of the
database schema. Without taking a significant performance hit.
--
_/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/
_/ _/
_/ Gary Dunn _/
_/ Open Slate Project _/
_/ http://openslate.sourceforge.net/ _/
_/ http://www.aloha.com/~knowtree/ _/
_/ Honolulu _/
_/ registered Linux user #273809 _/
_/ _/
_/ This tagline is umop apisdn. _/
_/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/
---------------------------------------------
This message was sent using Endymion MailMan.
http://www.endymion.com/products/mailman/
More information about the LUAU
mailing list