Introduction to DBI
Databases
- Relational
- SQL
- Examples:
- Oracle
- Informix
- Sybase
- Ingres
- Postgres
- mSQL
- MySQL
- It's hard to write portable DB apps:
- The SQL spec is incomplete, so each vendor
implements proprietary extensions
- No standard for front ends
- No standard for APIs
- The Bad Old Days: oraperl, sybperl, ingperl
- Vendor-specific APIs for other languages, too
- An application could work with only one RDBMS
- It was built on (and could use only) that vendor's API
DBI: A Vendor-Neutral Solution
- DBI
- Written and maintained by Tim Bunce
- Provides a single, common API that encapsulates each vendor's
specific API calls
- Example: The DBI call connect calls:
- Oracle's ora_login
- MySQL's mysql_connect
- Whatever's appropriate for any other database
- DBI is an object class
- Its sub-classes are a set of DBDs (DataBase Drivers), e.g.
- DBD::Oracle
- DBD::Informix
- DBD::mysql
- Usage:
#! /opt/perl5/bin/perl
use DBI; # Load the DBI
use DBD::mysql; # This program uses MySQL
- The payoff:
- If you write code that uses no RDBMS-specific calls...
- If you use only generic SQL...
- Then replacing one DBD with another is easy.
- You won't have to rewrite your program
when your pointy-haired boss makes you switch
databases.
Review
What are the prerequisites for writing
portable database applications?
Dan Keller Technical Services © 2000