CGI Programming with Perl by Scott Guelich

CGI Programming with Perl by Scott Guelich

Author:Scott Guelich [Scott Guelich, Shishir Gundavaram, and Gunther Birznieks]
Language: eng
Format: epub
Tags: COMPUTERS / Programming Languages / JavaScript
ISBN: 9781449326791
Publisher: O'Reilly Media
Published: 2011-12-31T16:00:00+00:00


Database manipulation

Database manipulation in DBI is quite simple. All you need to do is pass the create table, insert, update, or delete statement to the do method on the database handle. Immediately, the command will be executed:

$dbh->do( "insert into Player_Info values ('Hakeem Olajuwon', 10, 27, 11, 4, 2)") or die "Cannot do: " . $dbh->errstr( );

Database querying

Querying a database with DBI involves a few more commands since there are many ways in which you might want to retrieve data. The first step is to pass the SQL query to a prepare command. This will create a statement handle that is used to fetch the results:

my $sql = "select * from Player_Info"; my $sth = $dbh->prepare($sql) or die "Cannot prepare: " . $dbh->errstr( ); $sth->execute( ) or die "Cannot execute: " . $sth->errstr( ); my @row; while (@row = $sth->fetchrow_array( )) { print join(",", @row) . "\n"; } $sth->finish( );

Once the prepare command has been issued, the execute command is used to start the query. Since a query expects return results, we use a while loop to get each database record. The fetchrow_array command is used to fetch each row that is returned as an array of fields.

Finally, we clean up the statement handle by issuing the finish method. Note that in most cases we do not have to explicitly call the finish method. It is implicitly called by virtue of the fact that we have retrieved all the results. However, if the logic of your program decided to stop retrieving records before the entire statement had finished being retrieved, then calling finish is necessary in order to flush out the statement handle.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.