Database Reference
In-Depth Information
After installing the Cookbook.rb library file, try it from a test harness script, harness.rb :
#!/usr/bin/ruby -w
# harness.rb: test harness for Cookbook.rb library
require "Cookbook"
begin
dbh = Cookbook . connect
print "Connected \n "
rescue DBI : :DatabaseError => e
puts "Cannot connect to server"
puts "Error code: #{ e . err } "
puts "Error message: #{ e . errstr } "
exit ( 1 )
end
dbh . disconnect
print "Disconnected \n "
harness.rb has no require statement for the DBI module. It's unnecessary because the
Cookbook module itself imports DBI; any script that imports Cookbook also gains access
to DBI.
If you want a script to die if an error occurs without checking for an exception yourself,
write the script body like this:
dbh = Cookbook . connect
print "Connected \n "
dbh . disconnect
print "Disconnected \n "
PHP
PHP library files are written like regular PHP scripts. A Cookbook.php file that impleā€
ments a Cookbook class with a connect() method looks like this:
<? php
# Cookbook.php: library file with utility method for connecting to MySQL
# using the PDO module
class Cookbook
{
public static $host_name = "localhost" ;
public static $db_name = "cookbook" ;
public static $user_name = "cbuser" ;
public static $password = "cbpass" ;
# Establish a connection to the cookbook database, returning a database
# handle. Raise an exception if the connection cannot be established.
# In addition, cause exceptions to be raised for errors.
public static function connect ()
{
Search WWH ::




Custom Search