www.developer.com/lang/article.php/2184681
|
April 10, 2003 Data Source NamesWhen connecting to a database via the DBI, you need to tell the DBI where to find the database to connect to. For example, the database driver might require a database name, or a physical machine name upon which the database resides. This information is termed a data source name, and of all the aspects of DBI, this is possibly the most difficult to standardize due to the sheer number and diversity of connection syntaxes. The DBI requires the data source name to start with the characters For example, mSQL requires the hostname, database name, and potentially, the TCP/IP port number for connecting to the database server. However, Oracle may require only a single word that is an alias to a more complicated connection identifier that is stored in separate Oracle configuration files. DBI offers two useful methods for querying which data sources are available to you for each driver you have installed on your system. Firstly, you can get a list of all the available drivers installed on your machine by using the Secondly, you can invoke the The following script lists all the drivers and data sources for each driver on your system:
#!/usr/bin/perl -w
#
# ch04/listdsns: Enumerates all data sources and all
# installed drivers
#
use DBI;
### Probe DBI for the installed drivers
my @drivers = DBI->available_drivers();
die "No drivers found!\n" unless @drivers; # should never happen
### Iterate through the drivers and list the data sources for
### each one
foreach my $driver ( @drivers ) {
print "Driver: $driver\n";
my @dataSources = DBI->data_sources( $driver );
foreach my $dataSource ( @dataSources ) {
print "\tData Source is $dataSource\n";
}
print "\n";
}
exit;
The output from this script on my machine looks like:
Driver: ADO
Driver: CSV
Data source is DBI:CSV:f_dir=megaliths
Data source is DBI:CSV:f_dir=pictish_stones
Driver: ExampleP
Data Source is dbi:ExampleP:dir=.
Driver: File
Data Source is DBI:File:f_dir=megaliths
Data Source is DBI:File:f_dir=pictish_stones
Driver: ODBC
Driver: Proxy
Driver: XBase
Data Source is dbi:XBase:.
which tells us that we have the standard drivers While this may be interesting in theory, in practice you rarely need to use these methods. Most applications are written to use one data source name, either hardcoded into the application or passed in as a parameter in some way. When specifying a data source name for a database, the text following the driver prefix should be of the form that is appropriate for the particular database that you wish to connect to. This is very database-specific, but the following table shows some examples.[5]
In the case of the |