Python for Oracle Geeks or, “Just a wafer-thin slice of Python” or, “Python as a Second Language (PSL)” Catherine Devlin IntelliTech Systems
Outline ● Brainwashing ● Getting started ● Working with files ● Querying Oracle ● Building Web application
Me S. B. (sic) in Chemical Engineering Oracle DBA since1999 Pythonista since IntelliTech Systems Fairborn, OH
Tasks of a Database Administrator Task 1: Evaluate the Database Server Hardware Task 2: Install the Oracle Database Software Task 3: Plan the Database Task 4: Create and Open the Database Task 5: Back Up the Database Task 6: Enroll System Users Task 7: Implement the Database Design Task 8: Back Up the Fully Functional Database Task 9: Tune Database Performance Task 10: Download and Install Patches Task 11: Roll Out to Additional Hosts From Oracle® Database Administrator's Guide 10g Release 2 (10.2): “I work in the database.” Is that really all you do?
● Data files on filesystem ● Batch files and executables ● System resources (memory, CPU) ● Internet ● Object-oriented programming ● Complex text parsing Sometimes SQL and PL/SQL aren't enough
● Easy to learn and use ● Easy to read and maintain ● Powerful and versatile ● Free of cost! Dear Santa, I have been a very good DBA. For Christmas, I want a language that is... Consider a dynamic language (Perl, PHP, Ruby...)
Easy to learn and use Easy to read and maintain Very readable - “executable pseudocode” Unit testing Powerful and versatile Exception handling, XML Web publishing, Web browsing, Web services, GUI development... Free of cost!
Let's write something! Compare init.ora file to parameters in database db_block_size=8192 -OK- compatible= (live) (file) cursor_sharing=EXACT -OK- Web application
org DEMO
Pitfalls for the Oracle-minded 1. Everything is case-sensitive
object-orientation
Pitfalls for the Oracle-minded 1. Everything is case-sensitive 2. When calling a function, () is not optional
fileParams = { 'log_archive_start': True, 'db_block_size': 8192 } What we want is a table In Python, it's called a 'dictionary' and indicated by {}
STOP IT! Write a script already!
Pitfalls for the Oracle-minded 1. Everything is case-sensitive 2. When calling a function, () is not optional 3. No knee-jerk indenting!
Are you confused about where the “for” block begins and ends? Even though there is no END LOOP? Are you confused about where each command ends? Even though there are no semicolons? Python reads your code the same way your eye does!
Python core You have access without doing anything Standard library You must 'import' into a script or session External modules You must go get and install the code then import
1. Choose a web application platform Albatross, Aquarium, CherryPy, Cymbeline, Django, Draco, Impostor, JOTWeb, Karrigell, mod_python, Nevow, Paste, Python Servlet Engine, PyWork, QLime, Quixote, SkunkWeb, Snakelets, SnakeSkin, Subway, Twisted, TurboGears, WareWeb, WebWare, Zope
1. Choose a web application platform CherryPy – my favorite TurboGears – compare to Ruby on Rails (no Oracle support, yet) Zope – most popular; Web administration Plone – content management 2. Download and install 3. Read the CherryPy tutorial
Pitfalls for the Oracle-minded 1. Everything is case-sensitive 2. When calling a function, () is not optional 3. No knee-jerk indenting! 4. CherryPy tutorial is one version obsolete
Bind variables in cx_Oracle Use a :colon in the SQL string, then send a dictionary as second argument cursor.execute( “””SELECT hiredate FROM emp WHERE ename = :empName”””, {'empName':'SCOTT'})
What's next? Documentation > Tutorial Object-orientation Convenience wrapper: sqlWrap.py Object-relational mapping (a la Oracle TopLink) Exception handling, unit testing,...
References > Topic Guides > Databases tml API.html