Upload
jane-ashley
View
26
Download
1
Embed Size (px)
DESCRIPTION
Perl and ODBC. The First Annual Perl Conference. ODBC. ODBC stands for: O pen D ata B ase C onnectivity. ODBC. The ODBC standard was designed to work on any platform and has been ported to Win32, Unix, Macintosh, OS/2 and others. - PowerPoint PPT Presentation
Citation preview
The First AnnualPerl
Conference
Perl and ODBC
ODBC stands for:
Open DataBase Connectivity.
ODBC
• The ODBC standard was designed to work on any platform and has been ported to Win32, Unix, Macintosh, OS/2 and others.
• ODBC has become so accepted that some vendors like IBM, Informix and Watcom have designed their DBMS native programming interface based on ODBC.
His
tory
ODBC
ODBC was designed by:
• X/Open • SQL Access Group• ANSI• ISO• Microsoft• Digital• Sybase• IBM• Novell• Oracle• Lotus• and others. H
isto
ryODBC
Mod
els
ODBC
There are different ODBC models (or tiers) each describing the number of layers that must be passed through before the database is reached.
The three most common are:
•Tier 1
•Tier 2
•Tier 3
Mod
els
ODBC
Tier 1
(Tie
r 1)
ProgramProgram calls an ODBC function.
ODBC ManagerODBC Manager determines what to do.
ODBC DriverODBC Driver performs actual processing.
DatabaseFile
The database file is opened by the driver and data is manipulated.
Mod
els
ODBC
Tier 2
(Tie
r 2)
ProgramProgram calls an ODBC function.
ODBC ManagerODBC Manager determines what to do.
ODBC DriverODBC Driver prepares the request and passes it on to the DBMS.
Client
DatabaseFile
The DBMS processes the request.
DBMSServer
Server
Mod
els
ODBC(T
ier
3)
ProgramProgram calls an ODBC function.
ODBC ManagerODBC Manager determines what to do.
ODBC DriverODBC Driver prepares the request and passes it on to the DBMS.
Client
Server
Gateway
DatabaseFile
The DBMS processes the request.
DBMSServer
ODBC Manager/DriverGateway ODBC Manager/Driver pass the request on to the DMBS.
DS
NODBC
Data Source Name
DSN
Database InformationUseridPasswordConnection Information
= {
DS
NODBC
Data Source Name : User vs. System
DSN (aka User DSN) is only accessible by the user who created it.
System DSN is accessible by any user including the system itself.
Esc
ape
Seq
uen
ces
ODBC
ODBC Escape Sequences
•Outer Joins
•Scalar Functions
•Stored Procedures
•Date & Time Stamps
Syntax: {escape-token parameter}
{oj outer-join}
Esc
ape
Seq
uen
ces
ODBC
Outer Joins
SELECT * FROM {oj Machine LEFT OUTER JOIN Users ON Machine.Name = Users.Name}
where “outer-join” is:
tablename {LEFT | RIGHT | FULL} OUTER JOIN{tablename | outer-join} ON search-condition
where “function” is any of several functions:•Time Functions
•Character Functions
•Numeric Functions
{fn function}
Esc
ape
Seq
uen
ces
ODBC
Scalar Functions
{fn CurDate()}
{fn LTrim(FieldName)}
{fn Rand()}
• Calling a stored procedure.
• Calling a stored procedure with a return result.
{[?=] call procedure[(parameters…)]}
Esc
ape
Seq
uen
ces
ODBC
Stored Procedures
{call clean_database(db1)}
{? = call list_users}{? = copy_table( Table1, Table2)}
Date = {d ‘yyyy-mm-dd’}
Time = {t ‘hh:mm:ss’}
Timestamp = {ts ‘yyyy-mm-dd hh:mm:ss’}
Esc
ape
Seq
uen
ces
ODBC
Date & Time Stamps
{d ‘1997-08-20’}
{t ‘15:23:03’}
{ts ‘1997-08-20 15:23:03’}
Wh
y u
se it
Win32::ODBC
Why use Win32::ODBC?
•Easy to use
•Interface similar to the ODBC API
•Most ODBC functions are supported
•Full error reporting
•Object oriented model
Alt
ern
ativ
esWin32::ODBC
Alternatives to Win32::ODBC
•DBI interface by Tim Bunce
•IODBC Perl module by Brian Jepson
•ODBCTable by Evangelo Prodromou
Inst
alla
tion
Win32::ODBC
How to install Win32::ODBC
1) Create the directory:c:\perl\lib\auto\win32\odbc
Assuming Perl is installed in c:\perl
2) Copy ODBC.PLL into the new directory.
3) Copy ODBC.PM into:c:\perl\lib\win32
Usi
ng
the
exte
nsi
onWin32::ODBC
Loading the extension
use Win32::ODBC;
Before using Win32::ODBC you must load the extension into Perl:
Usi
ng
the
exte
nsi
onWin32::ODBC
How to use the Win32::ODBC extension
1) Connect to the database
2) Submit a query
3) Process the result
4) Close the database
$db = new Win32::ODBC(“My DSN”);
Con
nec
tin
gWin32::ODBC
Connecting to a database
Make a new connection to a DSN:
You can specify userid & passwords:
$DSN = “DSN=My DSN;UID=Dave;PWD=1234”;
$db = new Win32::ODBC($DSN);
Con
nec
tin
gWin32::ODBC
Connecting to a database
If the connection succeeds the result will be an object otherwise it will be undef:
if (! $db = new Win32::ODBC($DSN)){…process error…
}
II
Sub
mit
ting
a Q
uery
Win32::ODBC
Submitting a Query
if ($db->Sql(“SELECT * FROM Foo”)){…process error…
}
To submit a SQL query use the Sql() method:
Sql() returns undef if theSql() returns undef if thequery is successful.query is successful.
Pro
cess
ing
Res
ults
Win32::ODBC
Processing Results
while ($db->FetchRow())…process results…
}
To retrieve a row from a dataset use the FetchRow() method:
FetchRow() returns a 1 if FetchRow() returns a 1 if a row was successfully a row was successfully
retrieved.retrieved.
Pro
cess
ing
Res
ults
Win32::ODBC
Processing Results
undef %Data;%Data = $db->DataHash();
OR
undef %Data;
%Data = $db->DataHash(“Name”, “Age”);
Once a row has been fetched you need to extract data with the DataHash() method:
II
Clo
sing
Win32::ODBC
Closing The Database
$db->Close();
Once the processing of the data has completed, close the connection to the database:
Err
or P
roce
ssin
gWin32::ODBC
Processing Errors
print ”Error: “ . $db->Error();
If an error occurs you can determine the nature of the error with the Error() method:
Err
or P
roce
ssin
gWin32::ODBC
Processing Errors
$Error = Win32::ODBC::Error();
A call to Win32::ODBC::Error() will return the last error that occurred regardless of what connection generated it:
II
Err
or P
roce
ssin
gWin32::ODBC
Processing Errors
@Error = $db->Error();
The Error() method returns either an array or a string depending upon the context of the return:
III
$Error = $db->Error();
Retrieving an array of errors:
Retrieving an error string:
Err
or P
roce
ssin
gWin32::ODBC
Processing ErrorsThe array context will return:
IV
1) ODBC Error Number2) Tagged Text3) Connection Number4) SQLState
The string context will return:“[ErrorNum] [Connection] [SQLState] [Text]”
CG
IWin32::ODBC
•Use System DSN’s
•Give proper permissions on files
•Give proper access to database
Use with a CGI script
Got
cha’
sWin32::ODBC
Escaping the apostrophe
Common Gotcha’s
SELECT * FROM FooWHERE Name like ‘Joe’s’
SELECT *FROM FooWHERE Name like ‘Joe’’s’
Got
cha’
sWin32::ODBC
Determining Delimiters:
Common Gotcha’s
if ($db->GetTypeInfo(SQL_CHAR)){
$db->FetchRow();
($Pre, $Suf) = $db->Data(“LITERAL_PREFIX”,“LITERAL_SUFFIX”);
}
print “$Pre$Text$Suf”;
II
Got
cha’
sWin32::ODBC
• There are over 650 constants so only a few are exported into the main namespace.
Common Gotcha’s III
$db->SQL_CHAR
To use a constant either refer it through your object:
Or as function through the namespace:Win32::ODBC::SQL_CHAR()
Sho
rtcu
tsWin32::ODBC
Win32::ODBC reserves the ODBC namespace; functions can be accessed as:
Shortcuts
$db = new ODBC(“My DSN”);
$db = new Win32::ODBC(“My DSN”);
…or...
In other words, the namespaces ODBC and Win32::ODBC are synonymous.
Mor
e In
form
atio
nWin32::ODBC
Visit the Win32::ODBC Home Page:
More Information...
http://www.roth.net/odbc/
Win32::ODBC FAQ:
http://www.roth.net/odbc/odbcfaq.htm
Roth Consulting:
http://www.roth.net/consult/