Job Search

Monday, August 14, 2017

Setting up Oracle, Python and Eclipse


Installing Python 3 and Eclipse for Windows

Note:  Keep in mind that PyDev 5.x requires Eclipse 4.6 onwards (for Eclipse 3.8 use PyDev 4.x).
The highest version of 4.x listed is 4.5.5; we can copy the link http://www.pydev.org/update_sites/4.5.5 and paste into eclipse's Install New Software

Steps to configure Python with Eclipse:
Step 1: Install java
Step 2: Install Python 3
Step 3: Install or Extract eclipse

Ø Open Eclipse -> Help -> Install New Software... -> Add
·  Name: PyDev
·  Location: http://pydev.org/updates (or specific path for your eclipse version)

Ø Click OK
Ø Select "PyDev for Eclipse" and remain all default
Ø Click Next -> review the items to be installed -> Next
Ø Check "I accept the terms of the license agreement -> Finish
Ø Accept the certificates -> Click OK
Ø Restart Eclipse
Ø Open Eclipse -> Windows -> Preferences -> expand "PyDev" -> Interpreter - Python --> click on New and select the python executable (python.exe) from the python installed location -> Click OK -> Click OK
Ø General -> Editors -> Text Editors -> check Show Line Numbers -> OK (and it's going to go in and configure Python.)
Ø Under Perspectives -> Other -> select the "PyDev" perspective, because that's what we're going to be using.
Ø Right click on Java -> close, if java development is not required.

Now we're ready to create and configure our new python project with eclipse.

Python Interpreter Configuration
Ø  Select Window -> Preferences -> PyDev -> Interpreters -> Python Interpreter -> New (or Auto-Config).
Ø  Select location of python.exe for which cx_Oracle has been configured (32 Bit or 64 Bit), check oracle database server or client version using
Ø  Run-> cmd -> tnsping
Ø  Apply - > OK

Python cx_Oracle Configuration
Ø  Select Window -> Preferences -> PyDev -> Interpreters -> Python Interpreter -> Forced Builtins
Ø  New -> cx_Oracle -> OK


Python Oracle Project

Ø  Select File -> New-> PyDev Project
Ø  Project Name -> MyDemo
Ø  Project Type -> Python
Ø  Grammar Version -> 3-3.5
Ø  Interpreter -> Python
Ø  Finish

Ø  Right Click on Project Name (MyDemo)
Ø  New-> PyDev Module
Ø  Name -> DBTest

Paste below code in DBTest.py file

# Code start
import cx_Oracle
class MyClass(object):
    '''
    classdocs
    '''


    def __init__(self):
        con = cx_Oracle.connect('hr/hr@orcl')
        # do some stuff here
        #ver = con.version.split(".")
        #print(ver)
        #print(con.version)
        cur = con.cursor()
  cur.execute('select e.employee_id,e.first_name,e.last_name from  EMPLOYEES e where rownum<2 order by 2')
        for result in cur:
            #print(result)
            print(result[0],",",result[1]," ",result[2])
        cur.close()   
        con.close()
        '''
        Constructor
        '''

MyClass()      
# Code end

Execute Python Module

Run -> Run As -> 1 Python Run

Output
Console

100 , First_Name Last_Name


I hope you all have enjoyed reading this article. Comments are welcome....


Related Posts:
- Connect Python 3.6 with Oracle 12c on Windows 7 x64
- Oracle subprograms and Python Programming

Tuesday, August 8, 2017

Connect Python 3.6 with Oracle 12c on Windows 7 x64

To use Python with Oracle below components must all be in place and be of the same version (Python 3.6 suggested) and same architecture (64-bit suggested).

Windows 7 x64
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
Python 3.6 x64 (python-3.6.0-amd64.exe)
cx_Oracle (cx_Oracle-5.3-12c.win-amd64-py3.6-2.exe)

Instance running in 64-bit. To do this, we must have installed:
  • Part 1: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
Or Oracle 12c 64-bit Windows Instant Client 
  • Part 2: Python 3.6 x64 (python-3.6.0-amd64.exe) (64bit)
  • Part 3: cx_Oracle (cx_Oracle-5.3-12c.win-amd64-py3.6-2.exe) (64bit)

Part 1: Install Oracle Database 12c
Download and install Oracle Database 12c from below location


or
Download and install Oracle Database 12c Instant Client from below location


Part 2: Install Python 3.6 x64
Download and install Python 3.6 x64 (Windows x86-64 executable installer) from below location


Part 3: Install Python interface to Oracle (cx_Oracle-5.3-12c.win-amd64-py3.6-2.exe)
Download and install Python interface to Oracle (cx_Oracle-5.3-12c.win-amd64-py3.6-2.exe) from below location

Does it Work?
Now it is time to test the connection. We can execute the following code to test the connectivity.

Steps
Step 1: Create db_conn_test.py file

import cx_Oracle
con = cx_Oracle.connect('hr/hr@orcl')
# do some stuff here
#ver = con.version.split(".")
#print(ver)
print(con.version)
con.close()

Step 2:  open IDLE (Python 3.6 64-bit)
File -> Open -> select db_conn_test.py file

Step 3:
Run -> Run Module

Output:
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
============ RESTART: D:\Study\Python\Python_code\db_conn_test.py ============
12.1.0.2.0
>>> 

It will display current oracle database version.


Example: Display data from HR schema tables

import cx_Oracle
con = cx_Oracle.connect('hr/hr@orcl')
# do some stuff here
ver = con.version.split(".")
print(ver)
cur = con.cursor()
cur.execute('select * from EMPLOYEES e where e.employee_id < 150 order by 2')
for result in cur:
    print(result)
cur.close()          
con.close()



I hope you all have enjoyed reading this article. Comments are welcome....


Related Posts:
- Oracle subprograms and Python Programming