NEVUG - August 2004 To Boldly Go: Using Perl and the Perl DBI as an Adjunct to MS Access for Custom...

19
NEVUG - August 2004 To Boldly Go: Using Perl and the Perl DBI as an Adjunct to MS Access for Custom Reports Maggie Rioux MBLWHOI Library Woods Hole, Mass.

Transcript of NEVUG - August 2004 To Boldly Go: Using Perl and the Perl DBI as an Adjunct to MS Access for Custom...

Page 1: NEVUG - August 2004 To Boldly Go: Using Perl and the Perl DBI as an Adjunct to MS Access for Custom Reports Maggie Rioux MBLWHOI Library Woods Hole, Mass.

NEVUG - August 2004

To Boldly Go: Using Perl and the Perl DBI as an Adjunct

to MS Access for Custom Reports

Maggie Rioux

MBLWHOI Library

Woods Hole, Mass.

Page 2: NEVUG - August 2004 To Boldly Go: Using Perl and the Perl DBI as an Adjunct to MS Access for Custom Reports Maggie Rioux MBLWHOI Library Woods Hole, Mass.

NEVUG - August 2004

Using Perl and the Perl DBI as an Adjunct to MS Access for

Custom Reports

Page 3: NEVUG - August 2004 To Boldly Go: Using Perl and the Perl DBI as an Adjunct to MS Access for Custom Reports Maggie Rioux MBLWHOI Library Woods Hole, Mass.

NEVUG - August 2004

What is the Perl DBI

An add-on Perl moduleYou already have it as part of the

standard Voyager installLets you write a Perl program that talks

to the Oracle tables just like MS Access does from your PC

Then you can use all the power of Perl to play with the results

Page 4: NEVUG - August 2004 To Boldly Go: Using Perl and the Perl DBI as an Adjunct to MS Access for Custom Reports Maggie Rioux MBLWHOI Library Woods Hole, Mass.

NEVUG - August 2004

How to Use the Perl DBI

Develop your query using MS AccessTranslate the Access SQL into Oracle

SQLInterfacing the Perl with Oracle is all in

the book: Programming the Perl DBIAutomate running the program using

cron

Page 5: NEVUG - August 2004 To Boldly Go: Using Perl and the Perl DBI as an Adjunct to MS Access for Custom Reports Maggie Rioux MBLWHOI Library Woods Hole, Mass.

NEVUG - August 2004

And now some examples:

Page 6: NEVUG - August 2004 To Boldly Go: Using Perl and the Perl DBI as an Adjunct to MS Access for Custom Reports Maggie Rioux MBLWHOI Library Woods Hole, Mass.

NEVUG - August 2004

Active Holds/Recalls:The Access Report

Page 7: NEVUG - August 2004 To Boldly Go: Using Perl and the Perl DBI as an Adjunct to MS Access for Custom Reports Maggie Rioux MBLWHOI Library Woods Hole, Mass.

NEVUG - August 2004

Active Holds/Recalls:The Access SQL

SELECT [LAST_NAME]+", "+[FIRST_NAME] AS Name, WHOIDB_BIB_TEXT.TITLE, WHOIDB_MFHD_MASTER.DISPLAY_CALL_NO, WHOIDB_ITEM_BARCODE.ITEM_BARCODE, WHOIDB_HOLD_RECALL.HOLD_RECALL_TYPE, WHOIDB_HOLD_RECALL.CREATE_DATE, WHOIDB_PATRON_ADDRESS.ADDRESS_LINE1FROM ((WHOIDB_ITEM_BARCODE INNER JOIN ((WHOIDB_BIB_MFHD INNER JOIN (WHOIDB_MFHD_ITEM INNER JOIN ((WHOIDB_HOLD_RECALL INNER JOIN WHOIDB_HOLD_RECALL_ITEMS ON WHOIDB_HOLD_RECALL.HOLD_RECALL_ID = WHOIDB_HOLD_RECALL_ITEMS.HOLD_RECALL_ID) INNER JOIN WHOIDB_PATRON ON WHOIDB_HOLD_RECALL.PATRON_ID = WHOIDB_PATRON.PATRON_ID) ON WHOIDB_MFHD_ITEM.ITEM_ID = WHOIDB_HOLD_RECALL_ITEMS.ITEM_ID) ON WHOIDB_BIB_MFHD.MFHD_ID = WHOIDB_MFHD_ITEM.MFHD_ID) INNER JOIN WHOIDB_BIB_TEXT ON WHOIDB_BIB_MFHD.BIB_ID = WHOIDB_BIB_TEXT.BIB_ID) ON WHOIDB_ITEM_BARCODE.ITEM_ID = WHOIDB_HOLD_RECALL_ITEMS.ITEM_ID) INNER JOIN WHOIDB_MFHD_MASTER ON WHOIDB_MFHD_ITEM.MFHD_ID = WHOIDB_MFHD_MASTER.MFHD_ID) INNER JOIN WHOIDB_PATRON_ADDRESS ON WHOIDB_PATRON.PATRON_ID = WHOIDB_PATRON_ADDRESS.PATRON_IDWHERE (((WHOIDB_ITEM_BARCODE.BARCODE_STATUS)="1") AND ((WHOIDB_HOLD_RECALL_ITEMS.HOLD_RECALL_STATUS)="1") AND ((WHOIDB_PATRON_ADDRESS.ADDRESS_TYPE)="3"))ORDER BY [LAST_NAME]+", "+[FIRST_NAME];

Page 8: NEVUG - August 2004 To Boldly Go: Using Perl and the Perl DBI as an Adjunct to MS Access for Custom Reports Maggie Rioux MBLWHOI Library Woods Hole, Mass.

NEVUG - August 2004

Active Holds/Recalls:The Perl DBI/Oracle SQL

$SQL = "SELECT WHOIDB.PATRON.LAST_NAME, WHOIDB.PATRON.FIRST_NAME, WHOIDB.BIB_TEXT.TITLE, WHOIDB.MFHD_MASTER.DISPLAY_CALL_NO, WHOIDB.ITEM_BARCODE.ITEM_BARCODE, WHOIDB.HOLD_RECALL.HOLD_RECALL_TYPE, WHOIDB.HOLD_RECALL.CREATE_DATE, WHOIDB.PATRON_ADDRESS.ADDRESS_LINE1 FROM WHOIDB.ITEM_BARCODE, WHOIDB.BIB_MFHD, WHOIDB.MFHD_ITEM, WHOIDB.HOLD_RECALL, WHOIDB.HOLD_RECALL_ITEMS, WHOIDB.BIB_TEXT, WHOIDB.MFHD_MASTER, WHOIDB.PATRON_ADDRESS, WHOIDB.PATRON …….

Page 9: NEVUG - August 2004 To Boldly Go: Using Perl and the Perl DBI as an Adjunct to MS Access for Custom Reports Maggie Rioux MBLWHOI Library Woods Hole, Mass.

NEVUG - August 2004

Active Holds/Recalls:The Perl DBI/Oracle SQL

……WHERE WHOIDB.HOLD_RECALL.HOLD_RECALL_ID = WHOIDB.HOLD_RECALL_ITEMS.HOLD_RECALL_ID AND WHOIDB.HOLD_RECALL.PATRON_ID = WHOIDB.PATRON.PATRON_ID AND WHOIDB.MFHD_ITEM.ITEM_ID = WHOIDB.HOLD_RECALL_ITEMS.ITEM_ID AND WHOIDB.BIB_MFHD.MFHD_ID = WHOIDB.MFHD_ITEM.MFHD_ID AND WHOIDB.BIB_MFHD.BIB_ID = WHOIDB.BIB_TEXT.BIB_ID AND WHOIDB.ITEM_BARCODE.ITEM_ID = WHOIDB.HOLD_RECALL_ITEMS.ITEM_ID AND WHOIDB.MFHD_ITEM.MFHD_ID = WHOIDB.MFHD_MASTER.MFHD_ID AND WHOIDB.PATRON.PATRON_ID = WHOIDB.PATRON_ADDRESS.PATRON_ID AND WHOIDB.ITEM_BARCODE.BARCODE_STATUS = '1' AND WHOIDB.HOLD_RECALL_ITEMS.HOLD_RECALL_STATUS = '1' AND WHOIDB.PATRON_ADDRESS.ADDRESS_TYPE = '3' ORDER BY WHOIDB.PATRON.LAST_NAME, WHOIDB.PATRON.FIRST_NAME";

Page 10: NEVUG - August 2004 To Boldly Go: Using Perl and the Perl DBI as an Adjunct to MS Access for Custom Reports Maggie Rioux MBLWHOI Library Woods Hole, Mass.

NEVUG - August 2004

Active Holds/Recalls:The Meat of the Perl DBI Code

First set this important variable

Connect to the database

Set $SQL = the SQL you “fixed”

#set environment variable$ENV{ORACLE_HOME} = "/oracle/app/oracle/product/8.0.5";

# connect to whoidb$dbh = DBI->connect( "dbi:Oracle:LIBR", “xxxread", “Cxxx" ) || die "Can't cconnect to Oracle database: $DBI:errstr\n"

$SQL = "SELECT … ";

Page 11: NEVUG - August 2004 To Boldly Go: Using Perl and the Perl DBI as an Adjunct to MS Access for Custom Reports Maggie Rioux MBLWHOI Library Woods Hole, Mass.

NEVUG - August 2004

Active Holds/Recalls:The Meat of the Perl DBI Code

“Prepare” and execute

Deal with each returned line as an element in an array

Disconnect from the database when done

$sth = $dbh->prepare($SQL);$sth->execute();

while ( @row = $sth->fetchrow_array ) { …

# disconnect from database & close output file$dbh->disconnect || warn "Disconnection failed: $DBI:errstr\n";

Page 12: NEVUG - August 2004 To Boldly Go: Using Perl and the Perl DBI as an Adjunct to MS Access for Custom Reports Maggie Rioux MBLWHOI Library Woods Hole, Mass.

NEVUG - August 2004

Active Holds/Recalls: Perl Program Output

Page 13: NEVUG - August 2004 To Boldly Go: Using Perl and the Perl DBI as an Adjunct to MS Access for Custom Reports Maggie Rioux MBLWHOI Library Woods Hole, Mass.

NEVUG - August 2004

Patrons Leaving with Books Out:Output of the Access Query

Page 14: NEVUG - August 2004 To Boldly Go: Using Perl and the Perl DBI as an Adjunct to MS Access for Custom Reports Maggie Rioux MBLWHOI Library Woods Hole, Mass.

NEVUG - August 2004

Patrons Leaving with Books Out:The Access SQL

SELECT DISTINCTROW WHOIDB_PATRON.LAST_NAME AS [Last Name], WHOIDB_PATRON.FIRST_NAME AS [First Name], WHOIDB_PATRON.CURRENT_CHARGES AS [Books Out], WHOIDB_PATRON.EXPIRE_DATE AS Leaving, WHOIDB_PATRON_ADDRESS.ADDRESS_LINE1 AS [Address Info]FROM WHOIDB_PATRON INNER JOIN WHOIDB_PATRON_ADDRESS ON WHOIDB_PATRON.PATRON_ID = WHOIDB_PATRON_ADDRESS.PATRON_IDWHERE (((WHOIDB_PATRON.CURRENT_CHARGES)>"0") AND ((WHOIDB_PATRON.EXPIRE_DATE)<DateAdd("d",1,[Enter criterion date:])) AND ((WHOIDB_PATRON_ADDRESS.ADDRESS_TYPE)="1"))ORDER BY WHOIDB_PATRON.EXPIRE_DATE, WHOIDB_PATRON.LAST_NAME, WHOIDB_PATRON.FIRST_NAME;

Page 15: NEVUG - August 2004 To Boldly Go: Using Perl and the Perl DBI as an Adjunct to MS Access for Custom Reports Maggie Rioux MBLWHOI Library Woods Hole, Mass.

NEVUG - August 2004

Patrons Leaving with Books Out:Editing the Access SQL

Step 1 - Separate lines, change all WHOIDB_ to WHOIDB., take out the field labels (AS […])

SELECT DISTINCTROW WHOIDB.PATRON.LAST_NAME, WHOIDB.PATRON.FIRST_NAME, WHOIDB.PATRON.CURRENT_CHARGES, WHOIDB.PATRON.EXPIRE_DATE, WHOIDB.PATRON_ADDRESS.ADDRESS_LINE1, FROM WHOIDB.PATRON INNER JOIN WHOIDB_PATRON_ADDRESS ON WHOIDB.PATRON.PATRON_ID = WHOIDB.PATRON_ADDRESS.PATRON_IDWHERE (((WHOIDB.PATRON.CURRENT_CHARGES)>"0") AND ((WHOIDB.PATRON.EXPIRE_DATE)<DateAdd("d",1,[Enter criterion date:])) AND ((WHOIDB.PATRON_ADDRESS.ADDRESS_TYPE)="1"))ORDER BY WHOIDB.PATRON.EXPIRE_DATE, WHOIDB.PATRON.LAST_NAME, WHOIDB.PATRON.FIRST_NAME;

Page 16: NEVUG - August 2004 To Boldly Go: Using Perl and the Perl DBI as an Adjunct to MS Access for Custom Reports Maggie Rioux MBLWHOI Library Woods Hole, Mass.

NEVUG - August 2004

Patrons Leaving with Books Out:Editing the Access SQL

Step 2 - Change DISTINCTROW to DISTINCT or possibly UNIQUE, but probably not necessary in this

case since there’s an address type used Step 3 - Change the joins to regular SQL format

Page 17: NEVUG - August 2004 To Boldly Go: Using Perl and the Perl DBI as an Adjunct to MS Access for Custom Reports Maggie Rioux MBLWHOI Library Woods Hole, Mass.

NEVUG - August 2004

Patrons Leaving with Books Out:Editing the Access SQL

SELECT WHOIDB.PATRON.FIRST_NAME, WHOIDB.PATRON.CURRENT_CHARGES, WHOIDB.PATRON.EXPIRE_DATE, WHOIDB.PATRON_ADDRESS.ADDRESS_LINE1FROM WHOIDB.PATRON, WHOIDB.PATRON_ADDRESSWHERE WHOIDB.PATRON.PATRON_ID = WHOIDB.PATRON_ADDRESS.PATRON_ID AND WHOIDB.PATRON.CURRENT_CHARGES > ’0’ AND WHOIDB.PATRON.EXPIRE_DATE < $cutoffdate AND WHOIDB.PATRON_ADDRESS.ADDRESS_TYPE = ‘1’ORDER BY WHOIDB.PATRON.EXPIRE_DATE, WHOIDB.PATRON.LAST_NAME, WHOIDB.PATRON.FIRST_NAME;

Page 18: NEVUG - August 2004 To Boldly Go: Using Perl and the Perl DBI as an Adjunct to MS Access for Custom Reports Maggie Rioux MBLWHOI Library Woods Hole, Mass.

NEVUG - August 2004

Patrons Leaving with Books Out:The Final Perl DBI SQL

Put this into a proper SQL statement = to a variable & also change $cutoffdate to something that actually works

$SQL = “SELECT WHOIDB.PATRON.FIRST_NAME, WHOIDB.PATRON.CURRENT_CHARGES, WHOIDB.PATRON.EXPIRE_DATE, WHOIDB.PATRON_ADDRESS.ADDRESS_LINE1FROM WHOIDB.PATRON, WHOIDB.PATRON_ADDRESSWHERE WHOIDB.PATRON.PATRON_ID = WHOIDB.PATRON_ADDRESS.PATRON_ID AND WHOIDB.PATRON.CURRENT_CHARGES > ’0’ AND WHOIDB.PATRON.EXPIRE_DATE < SYSDATE + $days_to_add AND WHOIDB.PATRON_ADDRESS.ADDRESS_TYPE = ‘1’ORDER BY WHOIDB.PATRON.EXPIRE_DATE, WHOIDB.PATRON.LAST_NAME, WHOIDB.PATRON.FIRST_NAME”;

Page 19: NEVUG - August 2004 To Boldly Go: Using Perl and the Perl DBI as an Adjunct to MS Access for Custom Reports Maggie Rioux MBLWHOI Library Woods Hole, Mass.

NEVUG - August 2004

Patrons Leaving with Books Out:Output of the Perl Program