The First Annual Perl Conference Perl and ODBC
ODBC  stands for: O pen  D ata B ase  C onnectivity . ODBC
<ul><li>The ODBC standard was designed to work on any platform and has been ported to Win32, Unix, Macintosh, OS/2 and oth...
<ul><li>ODBC was designed by: </li></ul><ul><li>X/Open  </li></ul><ul><li>SQL Access  Group </li></ul><ul><li>ANSI </li></...
Models ODBC There are different ODBC models (or tiers) each describing the number of layers that must be passed through be...
Models ODBC Tier 1 (Tier 1) Program Program calls an ODBC function. ODBC Manager ODBC Manager determines what to do. ODBC ...
Models ODBC Tier 2 (Tier 2) Client Server Program Program calls an ODBC function. ODBC Manager ODBC Manager determines wha...
Models ODBC (Tier 3) Client Server Gateway Program Program calls an ODBC function. ODBC Manager ODBC Manager determines wh...
DSN ODBC D ata  S ource  N ame DSN Database Information Userid Password Connection Information = {
DSN ODBC D ata  S ource  N ame : U ser vs.  S ystem DSN (aka User DSN) is only accessible by the user who created it. Syst...
Escape Sequences ODBC ODBC Escape Sequences <ul><li>Outer Joins </li></ul><ul><li>Scalar Functions </li></ul><ul><li>Store...
{oj  outer-join } Escape Sequences ODBC Outer Joins SELECT *  FROM {oj Machine LEFT OUTER JOIN Users ON Machine.Name = Use...
<ul><li>where “function” is any of several functions: </li></ul><ul><ul><li>Time Functions  </li></ul></ul><ul><ul><li>Cha...
<ul><li>Calling a stored procedure. </li></ul><ul><li>Calling a stored procedure with a return result. </li></ul>{[?=] cal...
Date  = {d ‘yyyy-mm-dd’} Time = {t ‘hh:mm:ss’} Timestamp = {ts ‘yyyy-mm-dd hh:mm:ss’} Escape Sequences ODBC Date & Time St...
Why use it Win32::ODBC Why use Win32::ODBC? <ul><li>Easy to use </li></ul><ul><li>Interface similar to the ODBC API </li><...
Alternatives Win32::ODBC Alternatives to Win32::ODBC <ul><li>DBI interface by Tim Bunce </li></ul><ul><li>IODBC Perl modul...
Installation Win32::ODBC How to install Win32::ODBC 1) Create the directory: c:perllibautowin32odbc Assuming Perl is insta...
Using the extension Win32::ODBC Loading the extension use Win32::ODBC; Before using Win32::ODBC you must load the extensio...
Using the extension Win32::ODBC How to use the Win32::ODBC extension 1) Connect to the database 2) Submit a query 3) Proce...
$db = new Win32::ODBC(“My DSN”); Connecting Win32::ODBC Connecting to a database Make a new connection to a DSN:  You can ...
Connecting Win32::ODBC Connecting to a database If the connection succeeds the result will be an object otherwise it will ...
Submitting a Query Win32::ODBC Submitting a Query if ($db->Sql(“SELECT * FROM Foo”)){ …process error… } To submit a SQL qu...
Processing Results Win32::ODBC Processing Results while ($db->FetchRow()) …process results… } To retrieve a row from a dat...
Processing Results Win32::ODBC Processing Results undef %Data; %Data = $db->DataHash(); OR undef %Data; %Data = $db->DataH...
Closing Win32::ODBC Closing The Database $db->Close(); Once the processing of the data has completed, close the connection...
Error Processing Win32::ODBC Processing Errors print ”Error: “ . $db->Error(); If an error occurs you can determine the na...
Error Processing Win32::ODBC Processing Errors $Error = Win32::ODBC::Error(); A call to Win32::ODBC::Error() will return t...
Error Processing Win32::ODBC Processing Errors @Error = $db->Error(); The Error() method returns either an array or a stri...
Error Processing Win32::ODBC Processing Errors The array context will return: IV 1) ODBC Error Number 2) Tagged Text 3) Co...
CGI Win32::ODBC <ul><li>Use System DSN’s </li></ul><ul><li>Give proper permissions on files </li></ul><ul><li>Give proper ...
Gotcha’s Win32::ODBC Escaping the apostrophe Common Gotcha’s SELECT *  FROM Foo WHERE Name like ‘Joe’s’ SELECT * FROM Foo ...
Gotcha’s Win32::ODBC Determining Delimiters: Common Gotcha’s if ($db->GetTypeInfo(SQL_CHAR)){ $db->FetchRow(); ($Pre, $Suf...
Gotcha’s Win32::ODBC <ul><li>There are over 650 constants so only a few are exported into the main namespace. </li></ul>Co...
Shortcuts Win32::ODBC Win32::ODBC reserves the ODBC namespace; functions can be accessed as: Shortcuts $db = new ODBC(“My ...
More Information Win32::ODBC Visit the Win32::ODBC Home Page: More Information... http://www.roth.net/odbc/ Win32::ODBC FA...
Upcoming SlideShare
Loading in...5
×

perlodbc.ppt

2,766

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,766
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

perlodbc.ppt

  1. 1. The First Annual Perl Conference Perl and ODBC
  2. 2. ODBC stands for: O pen D ata B ase C onnectivity . ODBC
  3. 3. <ul><li>The ODBC standard was designed to work on any platform and has been ported to Win32, Unix, Macintosh, OS/2 and others. </li></ul><ul><li>ODBC has become so accepted that some vendors like IBM, Informix and Watcom have designed their DBMS native programming interface based on ODBC. </li></ul>History ODBC
  4. 4. <ul><li>ODBC was designed by: </li></ul><ul><li>X/Open </li></ul><ul><li>SQL Access Group </li></ul><ul><li>ANSI </li></ul><ul><li>ISO </li></ul><ul><li>Microsoft </li></ul><ul><li>Digital </li></ul><ul><li>Sybase </li></ul><ul><li>IBM </li></ul><ul><li>Novell </li></ul><ul><li>Oracle </li></ul><ul><li>Lotus </li></ul><ul><li>and others. </li></ul>History ODBC
  5. 5. Models ODBC There are different ODBC models (or tiers) each describing the number of layers that must be passed through before the database is reached. <ul><li>The three most common are: </li></ul><ul><ul><li>Tier 1 </li></ul></ul><ul><ul><li>Tier 2 </li></ul></ul><ul><ul><li>Tier 3 </li></ul></ul>
  6. 6. Models ODBC Tier 1 (Tier 1) Program Program calls an ODBC function. ODBC Manager ODBC Manager determines what to do. ODBC Driver ODBC Driver performs actual processing. Database File The database file is opened by the driver and data is manipulated.
  7. 7. Models ODBC Tier 2 (Tier 2) Client Server Program Program calls an ODBC function. ODBC Manager ODBC Manager determines what to do. ODBC Driver ODBC Driver prepares the request and passes it on to the DBMS. Database File The DBMS processes the request. DBMS Server
  8. 8. Models ODBC (Tier 3) Client Server Gateway Program Program calls an ODBC function. ODBC Manager ODBC Manager determines what to do. ODBC Driver ODBC Driver prepares the request and passes it on to the DBMS. Database File The DBMS processes the request. DBMS Server ODBC Manager/Driver Gateway ODBC Manager/Driver pass the request on to the DMBS.
  9. 9. DSN ODBC D ata S ource N ame DSN Database Information Userid Password Connection Information = {
  10. 10. DSN ODBC D ata S ource N ame : U ser vs. S ystem DSN (aka User DSN) is only accessible by the user who created it. System DSN is accessible by any user including the system itself.
  11. 11. Escape Sequences ODBC ODBC Escape Sequences <ul><li>Outer Joins </li></ul><ul><li>Scalar Functions </li></ul><ul><li>Stored Procedures </li></ul><ul><li>Date & Time Stamps </li></ul>Syntax: {escape-token parameter}
  12. 12. {oj outer-join } Escape Sequences ODBC Outer Joins SELECT * FROM {oj Machine LEFT OUTER JOIN Users ON Machine.Name = Users.Name} <ul><li>where “outer-join” is: </li></ul><ul><ul><li>tablename {LEFT | RIGHT | FULL} OUTER JOIN{tablename | outer-join } ON search-condition </li></ul></ul>
  13. 13. <ul><li>where “function” is any of several functions: </li></ul><ul><ul><li>Time Functions </li></ul></ul><ul><ul><li>Character Functions </li></ul></ul><ul><ul><li>Numeric Functions </li></ul></ul>{fn function } Escape Sequences ODBC Scalar Functions {fn CurDate()} {fn LTrim(FieldName)} {fn Rand()}
  14. 14. <ul><li>Calling a stored procedure. </li></ul><ul><li>Calling a stored procedure with a return result. </li></ul>{[?=] call procedure [(parameters…)]} Escape Sequences ODBC Stored Procedures {call clean_database(db1)} {? = call list_users} {? = copy_table( Table1, Table2)}
  15. 15. Date = {d ‘yyyy-mm-dd’} Time = {t ‘hh:mm:ss’} Timestamp = {ts ‘yyyy-mm-dd hh:mm:ss’} Escape Sequences ODBC Date & Time Stamps {d ‘1997-08-20’} {t ‘15:23:03’} {ts ‘1997-08-20 15:23:03’}
  16. 16. Why use it Win32::ODBC Why use Win32::ODBC? <ul><li>Easy to use </li></ul><ul><li>Interface similar to the ODBC API </li></ul><ul><li>Most ODBC functions are supported </li></ul><ul><li>Full error reporting </li></ul><ul><li>Object oriented model </li></ul>
  17. 17. Alternatives Win32::ODBC Alternatives to Win32::ODBC <ul><li>DBI interface by Tim Bunce </li></ul><ul><li>IODBC Perl module by Brian Jepson </li></ul><ul><li>ODBCTable by Evangelo Prodromou </li></ul>
  18. 18. Installation Win32::ODBC How to install Win32::ODBC 1) Create the directory: c:perllibautowin32odbc Assuming Perl is installed in c:perl 2) Copy ODBC.PLL into the new directory. 3) Copy ODBC.PM into: c:perllibwin32
  19. 19. Using the extension Win32::ODBC Loading the extension use Win32::ODBC; Before using Win32::ODBC you must load the extension into Perl:
  20. 20. Using the extension Win32::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
  21. 21. $db = new Win32::ODBC(“My DSN”); Connecting Win32::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);
  22. 22. Connecting Win32::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
  23. 23. Submitting a Query 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 the query is successful.
  24. 24. Processing Results 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 a row was successfully retrieved.
  25. 25. Processing Results 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
  26. 26. Closing Win32::ODBC Closing The Database $db->Close(); Once the processing of the data has completed, close the connection to the database:
  27. 27. Error Processing Win32::ODBC Processing Errors print ”Error: “ . $db->Error(); If an error occurs you can determine the nature of the error with the Error() method:
  28. 28. Error Processing Win32::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
  29. 29. Error Processing Win32::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:
  30. 30. Error Processing Win32::ODBC Processing Errors The array context will return: IV 1) ODBC Error Number 2) Tagged Text 3) Connection Number 4) SQLState The string context will return: “ [ErrorNum] [Connection] [SQLState] [Text]”
  31. 31. CGI Win32::ODBC <ul><li>Use System DSN’s </li></ul><ul><li>Give proper permissions on files </li></ul><ul><li>Give proper access to database </li></ul>Use with a CGI script
  32. 32. Gotcha’s Win32::ODBC Escaping the apostrophe Common Gotcha’s SELECT * FROM Foo WHERE Name like ‘Joe’s’ SELECT * FROM Foo WHERE Name like ‘Joe’’s’
  33. 33. Gotcha’s Win32::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
  34. 34. Gotcha’s Win32::ODBC <ul><li>There are over 650 constants so only a few are exported into the main namespace. </li></ul>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()
  35. 35. Shortcuts Win32::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.
  36. 36. More Information Win32::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/
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×