360 likes | 483 Views
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.
E N D
Perl and ODBC The First AnnualPerlConference
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. History
ODBC was designed by: X/Open SQL Access Group ANSI ISO Microsoft Digital Sybase IBM Novell Oracle Lotus and others. ODBC History
ODBC There are different ODBC models (or tiers) each describing the number of layers that must be passed through before the database is reached. Models • The three most common are: • Tier 1 • Tier 2 • Tier 3
ODBC Program calls an ODBC function. Program ODBC Manager determines what to do. ODBC Manager ODBC Driver performs actual processing. ODBC Driver Database File The database file is opened by the driver and data is manipulated. Tier 1 (Tier 1) Models
ODBC Program calls an ODBC function. Program ODBC Manager determines what to do. ODBC Manager ODBC Driver prepares the request and passes it on to the DBMS. ODBC Driver DBMSServer Database File The DBMS processes the request. Tier 2 Client (Tier 2) Models Server
ODBC Program calls an ODBC function. Program ODBC Manager determines what to do. ODBC Manager ODBC Driver prepares the request and passes it on to the DBMS. ODBC Driver ODBC Manager/Driver Gateway ODBC Manager/Driver pass the request on to the DMBS. DBMSServer Database File The DBMS processes the request. Client (Tier 3) Models Gateway Server
ODBC Data Source Name { Database InformationUseridPasswordConnection Information = DSN DSN
ODBC Data Source Name : User vs. System DSN (aka User DSN) is only accessible by the user who created it. DSN System DSN is accessible by any user including the system itself.
ODBC ODBC Escape Sequences Syntax: {escape-token parameter} • Outer Joins • Scalar Functions • Stored Procedures • Date & Time Stamps Escape Sequences
ODBC Outer Joins {oj outer-join} • where “outer-join” is: • tablename {LEFT | RIGHT | FULL} OUTER JOIN{tablename | outer-join} ON search-condition Escape Sequences SELECT * FROM {oj Machine LEFT OUTER JOIN Users ON Machine.Name = Users.Name}
ODBC Scalar Functions {fn function} • where “function” is any of several functions: • Time Functions • Character Functions • Numeric Functions Escape Sequences {fn CurDate()} {fn LTrim(FieldName)} {fn Rand()}
ODBC Stored Procedures {[?=] call procedure[(parameters…)]} • Calling a stored procedure. • Calling a stored procedure with a return result. Escape Sequences {call clean_database(db1)} {? = call list_users}{? = copy_table( Table1, Table2)}
ODBC Date & Time Stamps Date = {d ‘yyyy-mm-dd’} Time = {t ‘hh:mm:ss’} Timestamp = {ts ‘yyyy-mm-dd hh:mm:ss’} {d ‘1997-08-20’} Escape Sequences {t ‘15:23:03’} {ts ‘1997-08-20 15:23:03’}
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 Why use it
Win32::ODBC Alternatives to Win32::ODBC • DBI interface by Tim Bunce • IODBC Perl module by Brian Jepson • ODBCTable by Evangelo Prodromou Alternatives
Win32::ODBC How to install Win32::ODBC Assuming Perl is installed in c:\perl 1) Create the directory:c:\perl\lib\auto\win32\odbc Installation 2) Copy ODBC.PLL into the new directory. 3) Copy ODBC.PM into:c:\perl\lib\win32
Win32::ODBC Loading the extension Before using Win32::ODBC you must load the extension into Perl: Using the extension use Win32::ODBC;
Win32::ODBC How to use the Win32::ODBC extension 1) Connect to the database Using the extension 2) Submit a query 3) Process the result 4) Close the database
Win32::ODBC Connecting to a database Make a new connection to a DSN: $db = new Win32::ODBC(“My DSN”); Connecting You can specify userid & passwords: $DSN = “DSN=My DSN;UID=Dave;PWD=1234”; $db = new Win32::ODBC($DSN);
Win32::ODBC II Connecting to a database If the connection succeeds the result will be an object otherwise it will be undef: Connecting if (! $db = new Win32::ODBC($DSN)){…process error…}
Win32::ODBC Submitting a Query To submit a SQL query use the Sql() method: if ($db->Sql(“SELECT * FROM Foo”)){ …process error…} Submitting a Query Sql() returns undef if thequery is successful.
Win32::ODBC Processing Results To retrieve a row from a dataset use the FetchRow() method: while ($db->FetchRow()) …process results…} Processing Results FetchRow() returns a 1 if a row was successfully retrieved.
Win32::ODBC II Processing Results Once a row has been fetched you need to extract data with the DataHash() method: Processing Results undef %Data;%Data = $db->DataHash(); OR undef %Data; %Data = $db->DataHash(“Name”, “Age”);
Win32::ODBC Closing The Database Once the processing of the data has completed, close the connection to the database: Closing $db->Close();
Win32::ODBC Processing Errors If an error occurs you can determine the nature of the error with the Error() method: Error Processing print ”Error: “ . $db->Error();
Win32::ODBC II Processing Errors A call to Win32::ODBC::Error() will return the last error that occurred regardless of what connection generated it: Error Processing $Error = Win32::ODBC::Error();
Win32::ODBC Processing Errors III The Error() method returns either an array or a string depending upon the context of the return: Error Processing Retrieving an array of errors: @Error = $db->Error(); Retrieving an error string: $Error = $db->Error();
Win32::ODBC Processing Errors IV The array context will return: 1) ODBC Error Number2) Tagged Text3) Connection Number4) SQLState Error Processing The string context will return: “[ErrorNum] [Connection] [SQLState] [Text]”
Win32::ODBC Use with a CGI script • Use System DSN’s • Give proper permissions on files • Give proper access to database CGI
Win32::ODBC Common Gotcha’s Escaping the apostrophe SELECT * FROM FooWHERE Name like ‘Joe’s’ Gotcha’s SELECT *FROM FooWHERE Name like ‘Joe’’s’
Win32::ODBC II Common Gotcha’s Determining Delimiters: if ($db->GetTypeInfo(SQL_CHAR)){ $db->FetchRow(); ($Pre, $Suf) = $db->Data(“LITERAL_PREFIX”,“LITERAL_SUFFIX”); } print “$Pre$Text$Suf”; Gotcha’s
Win32::ODBC III Common Gotcha’s • There are over 650 constants so only a few are exported into the main namespace. Gotcha’s To use a constant either refer it through your object: $db->SQL_CHAR Or as function through the namespace: Win32::ODBC::SQL_CHAR()
Win32::ODBC Shortcuts Win32::ODBC reserves the ODBC namespace; functions can be accessed as: $db = new Win32::ODBC(“My DSN”); Shortcuts …or... $db = new ODBC(“My DSN”); In other words, the namespaces ODBC and Win32::ODBC are synonymous.
Win32::ODBC More Information... Visit the Win32::ODBC Home Page: http://www.roth.net/odbc/ More Information Win32::ODBC FAQ: http://www.roth.net/odbc/odbcfaq.htm Roth Consulting: http://www.roth.net/consult/