[thelist] CF: Determining which DB used.

Andrew Chadwick andrew.chadwick at prnewswire.co.uk
Wed Sep 12 04:58:13 CDT 2001


On Wed, Sep 12, 2001 at 04:36:46AM -0400, Frank wrote:
> The apps I write are cross platform, thus different DBs come into 
> play. Access or SQL 7 on PC, and MySQL on Linux. Is there a way of 
> automatically determining what database I'm using without having to 
> resort to creating a global variable and changing it every time I 
> move my app? It's a little thing that would make the transitions 
> smoother.

Assuming you're writing with Perl and the DBI, and your DBs are on the
same machine as your server, write a separate Perl module for opening
database handles and pus single sub there which returns a database
handle. In there, try each DSN in turn until you have what you want:

	sub open_db
	{
		my $dbh;
		return ($dbh, "MySQL") if $dbh = DBI->connect
			  ("DBI:mysql:glot", "glot", "plank");
		return ($dbh, "ODBC") if $dbh = DBI->connect
			  ("DBI:ODBC:quex", "quex", "goodge");
		# ... any more possibilities?
		die "can't open db!";
	}

If you're using any server-specific SQL in your code, you might need
the database type as well, so return that as well as the db handle.

-- 
Andrew Chadwick, UNIX/Internet Programmer, PR Newswire Europe, Oxford
--
The views or opinions above are solely mine and are not necessarily those
of PR Newswire Europe. The message may contain privileged or confidential
information; if you are not a named recipient, notify me, and do not copy,
use, or disclose this message. <andrew.chadwick at prnewswire.co.uk>.




More information about the thelist mailing list