36
The First Annual Perl Conference Perl and ODBC

The First Annual Perl Conference

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

Page 1: The First Annual Perl Conference

The First AnnualPerl

Conference

Perl and ODBC

Page 2: The First Annual Perl Conference

ODBC stands for:

Open DataBase Connectivity.

ODBC

Page 3: The First Annual Perl Conference

• 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

Page 4: The First Annual Perl Conference

ODBC was designed by:

• X/Open • SQL Access Group• ANSI• ISO• Microsoft• Digital• Sybase• IBM• Novell• Oracle• Lotus• and others. H

isto

ryODBC

Page 5: The First Annual Perl Conference

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

Page 6: The First Annual Perl Conference

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.

Page 7: The First Annual Perl Conference

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

Page 8: The First Annual Perl Conference

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.

Page 9: The First Annual Perl Conference

DS

NODBC

Data Source Name

DSN

Database InformationUseridPasswordConnection Information

= {

Page 10: The First Annual Perl Conference

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.

Page 11: The First Annual Perl Conference

Esc

ape

Seq

uen

ces

ODBC

ODBC Escape Sequences

•Outer Joins

•Scalar Functions

•Stored Procedures

•Date & Time Stamps

Syntax: {escape-token parameter}

Page 12: The First Annual Perl Conference

{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

Page 13: The First Annual Perl Conference

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()}

Page 14: The First Annual Perl Conference

• 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)}

Page 15: The First Annual Perl Conference

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’}

Page 16: The First Annual Perl Conference

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

Page 17: The First Annual Perl Conference

Alt

ern

ativ

esWin32::ODBC

Alternatives to Win32::ODBC

•DBI interface by Tim Bunce

•IODBC Perl module by Brian Jepson

•ODBCTable by Evangelo Prodromou

Page 18: The First Annual Perl Conference

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

Page 19: The First Annual Perl Conference

Usi

ng

the

exte

nsi

onWin32::ODBC

Loading the extension

use Win32::ODBC;

Before using Win32::ODBC you must load the extension into Perl:

Page 20: The First Annual Perl Conference

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

Page 21: The First Annual Perl Conference

$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);

Page 22: The First Annual Perl Conference

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

Page 23: The First Annual Perl Conference

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.

Page 24: The First Annual Perl Conference

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.

Page 25: The First Annual Perl Conference

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

Page 26: The First Annual Perl Conference

Clo

sing

Win32::ODBC

Closing The Database

$db->Close();

Once the processing of the data has completed, close the connection to the database:

Page 27: The First Annual Perl Conference

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:

Page 28: The First Annual Perl Conference

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

Page 29: The First Annual Perl Conference

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:

Page 30: The First Annual Perl Conference

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]”

Page 31: The First Annual Perl Conference

CG

IWin32::ODBC

•Use System DSN’s

•Give proper permissions on files

•Give proper access to database

Use with a CGI script

Page 32: The First Annual Perl Conference

Got

cha’

sWin32::ODBC

Escaping the apostrophe

Common Gotcha’s

SELECT * FROM FooWHERE Name like ‘Joe’s’

SELECT *FROM FooWHERE Name like ‘Joe’’s’

Page 33: The First Annual Perl Conference

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

Page 34: The First Annual Perl Conference

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()

Page 35: The First Annual Perl Conference

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.

Page 36: The First Annual Perl Conference

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/