Automate Database Deployment with Python

Slides:



Advertisements
Similar presentations
Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
Advertisements

AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
Advantage Data Dictionary. agenda Creating and Managing Data Dictionaries –Tables, Indexes, Fields, and Triggers –Defining Referential Integrity –Defining.
ORACLE Lecture 1: Oracle 11g Introduction & Installation.
Embedded SQL John Ortiz. Lecture 15Embedded SQL2 Why Isn’t Interactive SQL Enough?  How to do this using interactive SQL?  Print a well-formatted transcript.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
Python Web Applications A KISS Introduction. Web Applications with Python Fetching, parsing, text processing Database client – mySQL, etc., for building.
“This presentation is for informational purposes only and may not be incorporated into a contract or agreement.”
Project Implementation for COSC 5050 Distributed Database Applications Lab1.
Phil Brewster  One of the first steps – identify the proper data types  Decide how data (in columns) should be stored and used.
SEMESTER 1, 2013/2014 DB2 APPLICATION DEVELOPMENT OVERVIEW.
ORACLE ONLINE TRAINING Contact our Support Team : SOFTNSOL India: Skype id : softnsoltrainings id:
Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.
Agenda Journalling More Embedded SQL. Journalling.
Overview of JDBC and Pro*C 1 Overview of JDBC,Pro*C and Oracle connectivity on Omega CSE 5330 – Database Systems.
Chapter Oracle Server An Oracle Server consists of an Oracle database (stored data, control and log files.) The Server will support SQL to define.
Announcements Read JDBC Project Step 5, due Monday.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
I Copyright © 2004, Oracle. All rights reserved. Introduction Copyright © 2004, Oracle. All rights reserved.
Python & Oracle Requirements Installation Oracle Instant Client
Overview of JDBC and Pro*C 1 CSE 5330 – Database Systems.
Stored procedures1 Stored procedures and functions Procedures and functions stored in the database.
CERN - IT Department CH-1211 Genève 23 Switzerland t DB Development Tools Benthic SQL Developer Application Express WLCG Service Reliability.
Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2014.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
Module 8: Implementing Stored Procedures. Overview Implementing Stored Procedures Creating Parameterized Stored Procedures Working With Execution Plans.
Programmatic SQL Shaista Khan CS 157B. Topic Embedded SQL statements in high-level programming languages.
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Stored Procedures. Definition a stored procedure is a set of Structured Query Language (SQL) statements with an assigned name that's stored in the database.
1 CS 430 Database Theory Winter 2005 Lecture 14: Additional SQL Topics.
1 Developing WBEM Clients Using Python Tim Potter Hewlett-Packard Company
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
PL/SQL programming Procedures and Cursors Lecture 1 [Part 2]
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
Python Dr. Maury Eggen Fall Introduction #!/usr/bin/python python is interpreted python is not strongly typed python allows multiple assignments.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
EGEE is a project funded by the European Union under contract IST Installation and configuration of gLite services Robert Harakaly, CERN,
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
E Copyright © 2006, Oracle. All rights reserved. Using SQL Developer.
C Copyright © 2009, Oracle. All rights reserved. Using SQL Developer.
CGS 3066: Web Programming and Design Spring 2017
Web Database Programming Using PHP
Managing, Storing, and Executing DTS Packages
Data Virtualization Tutorial: Introduction to SQL Script
Web Technologies IT230 Dr Mohamed Habib.
Introduction Python is an interpreted, object-oriented and high-level programming language, which is different from a compiled one like C/C++/Java. Its.
Web Database Programming Using PHP
PL/SQL.
22-INTEGRATION HUB
UNIT - V STORED PROCEDURE.
HW#4 Making Simple BBS Using JDBC
PHP Introduction.
DBM 405 Innovative Education- -snaptutorial.com
T-SQL Transact - Structured Query Language (Microsoft)
PL/SQL Scripting in Oracle:
Advanced PL/SQL Programing
Introduction to Python
Using JDeveloper.
Oracle Memory Internals
IntroductionToPHP Static vs. Dynamic websites
Intro to PHP.
Chapter 8 Advanced SQL.
MATERI PL/SQL Procedures Functions Packages Database Triggers
CodePainter Revolution Trainer Course
Presentation transcript:

Automate Database Deployment with Python By Michael McLaughlin Python cx_Oracle Tutorial cx_Oracle Documentation

Objectives Introduce Python Learn how to install cx_Oracle Learn how to connect to Oracle Learn how to run static SQL statements Learn how to run dynamic SQL statements Learn how to call PL/SQL store programs

Introduce Python

Python? A robust high-level programming language: Gets complex things done quickly Automate system and data integration tasks Solve complex analytical problems

Install cx_Oracle Driver

The cx_Oracle Driver Conforms to the Python DB API 2.0 Requires Oracle Client Installation Not presently in standard Linux repos You can download from here: https://pypi.python.org/pypi/cx_Oracle

Installing cx_Oracle Driver Things you need to know first: Ships for Linux/Unix in RPM file Ships for Windows in MS Windows Installer file Available for Python 2.6/2.7 on Linux/Unix Available for Python 2.6/2.7/3.4-3.6 on Windows Requires local Oracle Instant Client insall Supports platforms: Linux, Unix, Mac OS X, and Windows

Installation Steps (as root) Check your Python version with: # python -V Check for Oracle Client installation: # rpm –qa oracle-instantclient11-2-basic # yum install -y /tmp/oracle-instantclient* Install cx_Oracle Driver: # yum install -y cx_Oracle-5*

Verify cx_Oracle Installation Connect to the Python IDLE environment Type in the following commands: >>> import cx_Oracle >>> db = cx_Oracle.connect("student/student@xe") >>> print db.version Returns the following for Oracle 11g XE: 11.2.0.2.0

Connect to Oracle

Implement a connect.py script #!/usr/bin/python # Import the Oracle library. import cx_Oracle, sys try: # Create a connection. db = cx_Oracle.connect("student/student@xe") # Print a message. print "Connected to the Oracle " + db.version + " database." except cx_Oracle.DatabaseError, e: error, = e.args print >> sys.stderr, "Oracle-Error-Code:", error.code print >> sys.stderr, "Oracle-Error-Message:", error.message finally # Close connection. db.close();

Run a static SQL Statement

Implement a static query #!/usr/bin/python ... try: # Create a connection. db = cx_Oracle.connect("student/student@xe") # Create a cursor. cursor = db.cursor() # Execute a query. cursor.execute("SELECT 'Hello world!' FROM dual") # Read the contents of the cursor. for row in cursor: print (row[0]) ... finally: # Close cursor and connection. cursor.close() db.close()

Returning rows from a cursor #!/usr/bin/python ... # Execute a query. cursor.execute("SELECT item_title " + ", item_rating " + "FROM item " + "WHERE item_type = ” " (SELECT common_lookup_id " + " FROM common_lookup " + " WHERE common_lookup_type = 'DVD_WIDE_SCREEN')") # Read the contents of the cursor. for row in cursor: print (row[0], row[1]) ...

Run a dynamic SQL Statement

Binding a value in a cursor The local variable. #!/usr/bin/python ... sRate = 'PG-13' ... # Define a dynamic statment. stmt = "SELECT item_title, item_rating " + \ "FROM item WHERE item_rating = :rating” # Create a cursor. cursor = db.cursor() # Execute a statement with a bind variable. cursor.execute(stmt, rating = sRate) # Read the contents of the cursor. for row in cursor: print (row[0], row[1]) ... The bind variable. Name and value pair assignment.

Binding two values in a cursor The list of local variables. #!/usr/bin/python ... # Define a list. dvd = ('DVD_FULL_SCREEN','DVD_WIDE_SCREEN')| ... stmt = "SELECT common_lookup_id" + "\n" + \ "FROM common_lookup" + "\n" + \ "WHERE common_lookup_table = 'ITEM'" + "\n" + \ "AND common_lookup_column = 'ITEM_TYPE'" + "\n" + \ "AND common_lookup_type IN (:x,:y)” # Parse the statement by replacing line returns with a single # whitespace, replacing multiple whitespaces with single spaces. stmt = re.sub('\s+',' ',stmt.replace('\n',' ').replace('\r','')) # Declare a dynamic statement with a sequence. cursor.execute(stmt, x = dvd[0], y = dvd[1]) ... The bind variables. Name and value pair assignment.

Binding a dynamic dictionary A tuple used to name dictionary names, or the names of name-value pairs. #!/usr/bin/python ... # Define an alphabetic indexing tuple. ind = tuple('abcdefghijklmnopqrstuvwxyz') # Define a parameter list and empty target list. typ = ('DVD_FULL_SCREEN','DVD_WIDE_SCREEN','BLU-RAY') mat = {} ... A dynamic list of variables that need to bound to a dynamic SQL statement Declaring an empty dictionary.

Binding a dynamic dictionary Beginning of list of values. ... # Define a dynamic query. stmt = "SELECT common_lookup_id" + "\n" + \ ... "AND common_lookup_column = 'ITEM_TYPE'" + "\n" + \ "AND common_lookup_type IN (" # Build dictionary and append dynamic bind list to statement. for j, e in enumerate(typ): mat[str(ind[j])] = typ[j] if j == len(typ) - 1: stmt = stmt + ":" + str(ind[j]) else: stmt = stmt + ":" + str(ind[j]) + ", " # Close lookup value set. stmt = stmt + ")" + "\n" \ "ORDER BY 1” ... Logic to build list of bind variables and dynamic dictionary. Append closing parenthesis of list.

Dynamic statement and dictionary Generated dynamic statement: AND    common_lookup_column = 'ITEM_TYPE' AND    common_lookup_type IN (:a, :b, :c) ORDER BY 1 Parse the query for line returns: stmt = re.sub('\s+',' ',stmt.replace('\n',' ').replace('\r','')) Generated mat dictionary: {'a': 'DVD_FULL_SCREEN', 'c': 'BLU-RAY', 'b': 'DVD_WIDE_SCREEN'} Call the query with a dictionary:   cursor.execute(stmt, mat)

Assign local variables to bind targets. Binding a transaction ... # Declare variables. sItemTitle = 'Star Trek Beyond’ sItemSubtitle = 'Extended Edition' ... # Execute a query. stmt = "UPDATE item"                           + "\n" + \      "SET    item_subtitle = :bItemSubtitle" + "\n" + \      "WHERE  item_title = :bItemTitle" ... # Declare a dynamic statement. cursor.execute(stmt, bItemTitle = sItemTitle                        , bItemSubtitle = sItemSubtitle ) # Commit the inserted value. db.commit() ... Assign local variables to bind targets. Commit change.

Transaction Control You start a transaction like: # Set a starting transaction point. db.begin() Commit the write in the try-block: db.commit() Rollback in the except block with an error: db.rollback()

Call PL/SQL stored programs

Calling a procedure First call parameter is the name of the procedure. ... # Create a connection. db = cx_Oracle.connect("student/student@xe") # Create a cursor. cursor = db.cursor() # Call a stored procedure. cursor.callproc( 'insert_bill_detail' \ , ( sBillNumber \ , sBillText \ , sDetailNumber \ , sDetailText )) ... First call parameter is the name of the procedure. Second call parameter is the list of actual parameters.

Calling a procedure with list A parameter list. ... # Create a sequence for a procedure call. param = (sBillNumber, sBillText, sDetailNumber, sDetailText) try: # Create a connection. db = cx_Oracle.connect("student/student@xe” # Create a cursor. cursor = db.cursor() # Call a stored procedure. cursor.callproc( 'insert_bill_detail', param) ... Call parameters bundled in a list.

Calling a function with list ... # Create a sequence for a procedure call. param = (sBillNumber, sBillText, sDetailNumber, sDetailText) try: # Create a connection. db = cx_Oracle.connect("student/student@xe") # Create a cursor. cursor = db.cursor() # Call a stored procedure. fRetVal = cursor.callfunc( 'insert_bill_detail_func', cx_Oracle.NUMBER, param) # Check for successful function call or failure number. if fRetVal == 0: print "Success” else: print "Failure [" + str(int(fRetVal)) + "]” ... Function return type. Capture a failure code from the function.

Review Introduce Python Learn how to install cx_Oracle Learn how to connect to Oracle Learn how to run static SQL statements Learn how to run dynamic SQL statements Learn how to call PL/SQL store programs

Questions & Answers