| ★ wanayoo — archive 1999 http://wdvl.com/Authoring/DB/Intro/dbi_api.html | Nouvelle recherche | Portail wanayoo |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The DBI APIOctober 19, 1998
So how do you use the DBI module? Well, as with any Perl 5 module, you simply use the "USE" keyword. Once, the DBI module is loaded, you can then instantiate a database connection. Establishing a connection involves creating a "database handle" object by using DBI to connect to a database given a data source name, database user, database password, and a database driver. Consider the following example in which we connect to a Database with the data source name of "MyCompany" using the username "selena", password "12mw_l", and the ODBC driver. Notice that the ODBC driver name is tagged to the beginning of the Database name.
use DBI;
$dbHandle = (DBI->connect('DBI:ODBC:MyCompany',
'selena",
'12mw_l'));
We'll show you how to setup a database name etc when we cover Access later. However, you can imagine that regardless of what database you use on what system, you will be able to define these values and use them. Once you have created a database handle object, you can do things with the object. Most likely, you will be sending SQL statements to the database via the database handle. To do so, you create a statement handle object by calling the prepare() method on the database handle object and then call the execute() method on the statement handle object. Consider the following code:
use DBI;
$dbHandle = (DBI->connect('DBI:ODBC:MyCompany',
'selena",
'12mw_l'));
$sql = "SELECT * FROM Employees";
$statementHandle = $dbHandle->prepare($sql);
$statementHandle->execute() ||
die $statementHandle->errstr;
Once the sql has been sent to the database, the DBI module will store the results. To get to the results, you can use any number of useful statement handle methods. One of the most common methods is the fetchall_arrayref() method that returns all the returned database rows in a reference to an array of references to rows. Consider the following:
use DBI;
$dbHandle = (DBI->connect('DBI:ODBC:MyCompany',
'selena",
'12mw_l'));
$sql = "SELECT * FROM Employees";
$statementHandle = $dbHandle->prepare($sql);
$statementHandle->execute() ||
die $statementHandle->errstr;
$arrayRef = $statementHandle->fetchall_arrayref;
Of course, once you have the reference to the array, you can dereference everything to access the data. Finally, when you have massaged all the data, you close the database connection with the database handle object's disconnect() method:
$dbh->disconnect(); At the end of this part, I have included several examples of CGI script using DBI so you can see how you might build an application around the methods. Further, fantastic documentation for DBI can be found at http://www.hermetica.com/. However, I will also include a cursory API reference summary (based upon the DBI documentation by Tim Bunce) of the most used methods here so you can get a feel for what is available.
Note that there are all sorts of other more complex methods such as binding and error handling, but you should consult the documentation and the DBI tutorial referenced above. These topics are a bit beyond the scope of this tutorial.
Introduction to Databases for the Web | Table of Contents Getting the Pieces | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Instantly look up a "techie" word using
Webopedia!!
|
internet.com
| |
e-commerce
| |
WebDeveloper Network
|
Copyright 1999 internet.com Corporation. Legal Notices. Privacy Policy.