0% found this document useful (0 votes)
47 views

Python For Oracle DBA

python for database administrator
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Python For Oracle DBA

python for database administrator
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Beginner-friendly Python for

Oracle DBAs
- This beginner-friendly Python lecture will take you from zero to programming in Python.

"Opinions expressed are solely my own and do not express the views or opinions of my
employer."

© 2020 EPICO TECH


2
© 2020 EPICO TECH
3
© 2020 EPICO TECH
4
© 2020 EPICO TECH
http://www.oraworld.org

5
© 2020 EPICO TECH
What to expect from this
presentation and the reasons behind
it:

- The DBA role is evolving

- Automation is becoming more relevant

- Kick-start for Python programming language

- Oracle Database and multi-cloud is already a reality

6
© 2020 EPICO TECH
https://www.oracle.com/a/ocom/docs/applications/50704-oracle-strategic-dba-checklist.pdf

7
8
© 2020 EPICO TECH
9
© 2020 EPICO TECH
- What is Python Programming Language?
- Python is an interpreted, high-level, general-purpose programming language ;

- Guido van Rossum is the creator and the first released of Python was in 1991;

- Easy and intuitive language;

- Open Source

- Suitability for everyday tasks

- Python is case sensitive: BR <> br

- Comments #

- Similar PL/SQL but not the same. It's a plus if you already know PL/SQL.
© 2020 EPICO TECH
- What is Python Programming Language?
Basics concepts:
1 - Types :

1.1 INT: E.g 12


- Integers can be negative or positive;
- Finite range;

1.2 FLOAT :
- Real numbers: E.g 12.21
- Float can be an integer

© 2020 EPICO TECH


- What is Python Programming Language?
Basics concepts:
1 - Types :

1.3 STRINGS:
Description = "SKI IS BLUE"

1.4 BOOLEAN:
True (T) - 1
False (F) - 0

© 2020 EPICO TECH


- What is Python Programming Language?
- Typecasting

Integer 2 -> Float 2.0;


Float 1.1 -> Integer 1;

© 2020 EPICO TECH


- What is Python Programming Language?
Expressions

Example: 5+10=15
-

© 2020 EPICO TECH


- What is Python Programming Language?

Variables

© 2020 EPICO TECH


- What is Python Programming Language?

Tuples

- Here is a TUPLE "numbers":

numbers=(0,23,5,6,7,2,4,24)
- Tuples are immutable.

© 2020 EPICO TECH


- What is Python Programming Language?

Lists

- Here is a List "B":

B=['Lebron James', 15.2, 1975]


- List are mutable

© 2020 EPICO TECH


- What is Python Programming Language?

Dictionaries

{"key01":1,"key02":"02","key03":[3,3,3],"key04":(4,4,4),"key05":5}

© 2020 EPICO TECH


- What is Python Programming Language?

Sets

players_list=["Ronaldo","Real Madrid", "Real Madrid", 2001]


players_set=set(players_list)
players_set:{'Ronaldo','Real Madrid', 2001}

© 2020 EPICO TECH


- What is Python Programming Language?

Functions

- Python has many built-in functions.

© 2020 EPICO TECH


- What is Python Programming Language?
Objects

© 2020 EPICO TECH


- What is Python Programming Language?
Objects
- built-in __init__() function

© 2020 EPICO TECH


- What is Python Programming Language?
Objects methods
- built-in __init__() function

© 2020 EPICO TECH


24
© 2020 EPICO TECH
- What can you do with Python?
- https://www.python.org/

© 2020 EPICO TECH


- What can you do with Python?
- Install in Oracle Linux.
- Documentation: https://yum.oracle.com/oracle-linux-python.html

© 2020 EPICO TECH


- What can you do with Python?
- Install in Oracle Linux 6
- Documentation: http://yum.oracle.com/repo/OracleLinux/OL6/latest/x86_64/index.html

27
© 2020 EPICO TECH
- What can you do with Python?
- Install in Oracle Linux 7
- Documentation: http://yum.oracle.com/repo/OracleLinux/OL7/latest/x86_64/index.html

28
© 2020 EPICO TECH
What can you do with Python?
- Know the Python version in your system:
- Python on Oracle Linux 6.10

- Python on Oracle Linux 7.6

29
© 2020 EPICO TECH
- What can you do with Python?
1 - Connect to the Oracle Database.
1.1 - cx_Oracle x Installation :

- Documentation: https://cx-oracle.readthedocs.io/en/latest/

30
© 2020 EPICO TECH
- What can you do with Python?
- cx_Oracle x Installation – Pre-requisites:
• Install python version 3;

oraclelinux7]# python --version


Python 2.7.5

oraclelinux7]# yum install -y python3


Loaded plugins: langpacks, ulninfo
Resolving Dependencies
--> Running transaction check
---> Package python3.x86_64 0:3.6.8-13.0.1.el7 will be installed
--> Processing Dependency: python3-libs(x86-64) = 3.6.8-13.0.1.el7 for package: python3-3.6.8-13.0.1.el7.x86_64
--> Processing Dependency: python3-pip for package: python3-3.6.8-13.0.1.el7.x86_64
--> Processing Dependency: python3-setuptools for package: python3-3.6.8-13.0.1.el7.x86_64
--> Processing Dependency: libpython3.6m.so.1.0()(64bit) for package: python3-3.6.8-13.0.1.el7.x86_64
--> Running transaction check
---> Package python3-libs.x86_64 0:3.6.8-13.0.1.el7 will be installed
---> Package python3-pip.noarch 0:9.0.3-7.el7_8 will be installed
---> Package python3-setuptools.noarch 0:39.2.0-10.el7 will be installed

...
...
Dependency Installed:
python3-libs.x86_64 0:3.6.8-13.0.1.el7 python3-pip.noarch 0:9.0.3-7.el7_8 python3-setuptools.noarch
0:39.2.0-10.el7

Complete! 31
oraclelinux7]#
© 2020 EPICO TECH
- What can you do with Python?
- cx_Oracle x Installation – Pre-requisites:

• Install Cx_Oracle 7 from Pypi: https://pypi.org/project/cx-Oracle/

oraclelinux7] python3 -m pip install cx_Oracle --upgrade


Collecting cx_Oracle
Using cached
https://files.pythonhosted.org/packages/d5/15/d38862a4bd0e18d8ef2a3c98f39e743b8951ec
5efd8bc63e75db04b9bc31/cx_Oracle-7.3.0-cp36-cp36m-manylinux1_x86_64.whl
Installing collected packages: cx-Oracle
Successfully installed cx-Oracle-7.3.0

32
© 2020 EPICO TECH
- What can you do with Python?
- cx_Oracle x Installation – Pre-requisites:
• Install Oracle Instant Client:

Oracle Instant Client Zip Files


https://www.oracle.com/ae/database/technologies/instant-client/linux-x86-64-downloads.html

Oracle Instant Client RPMs


yum install oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm

(Example of Instant Client Installation for Oracle Database 12cR1):


oraclelinux7]# yum install oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm
Loaded plugins: langpacks, ulninfo
Examining oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm: oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64
Marking oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package oracle-instantclient12.1-basic.x86_64 0:12.1.0.2.0-1 will be installed
Installed:
oracle-instantclient12.1-basic.x86_64
...
Complete! 33
oraclelinux7]#
© 2020 EPICO TECH
- What can you do with Python?

- cx_Oracle x Installation – Pre-requisites:

• Add Instant Client to the runtime link path .

• oraclelinux7]# sh -c "echo /usr/lib/oracle/12.2/client64/lib > /etc/ld.so.conf.d/oracle-


instantclient.conf"
oraclelinux7]# ldconfig
oraclelinux7]$ export LD_LIBRARY_PATH=/usr/lib/oracle/12.1/client64/lib:$LD_LIBRARY_PATH

34
© 2020 EPICO TECH
- What can you do with Python?
1 - Connect to the Oracle Database.

1.2 - Install the Oracle Database software (Example of a 12Cr1 Oracle Database software installed
in a Oracle Linux 7.6):

SQL*Plus: Release 12.1.0.2.0 Production on Mon Jun 1 05:24:19 2020

Copyright (c) 1982, 2014, Oracle. All rights reserved.

Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options

SQL> select instance_name, status from v$instance;

INSTANCE_NAME STATUS
---------------- ------------
35
pythondb OPEN
© 2020 EPICO TECH
- What can you do with Python?
1 - Connect to the Oracle Database.
1.3 - Create the script to connect to the Oracle Database (Script called myconnectdb.py):
oraclelinux7]$ cat myconnectdb.py
from __future__ import print_function

import cx_Oracle

# Connect as user "hr" with password "welcome" to the "oraclepdb" service running on this computer.
connection = cx_Oracle.connect("c##brunors", "112233", "localhost.localdomain/pythondb.localdomain")

cursor = connection.cursor()
cursor.execute(
'SELECT count(*) from all_users')
for value in cursor:

print("Values:", value)

oraclelinux7]$ python3 myconnectdb.py


Values: (36,)

36
© 2020 EPICO TECH
- What can you do with Python?
2 - E.g Comparing files to be used by an Oracle External tables :
2.1 File:
File techtable1.txt:
oraclelinux7]$ cat techtable1.txt
BRZ,Brazil,Portuguese
NEZ,New Zealand,English
PRT,Portugal,Portuguese
SPN,Spain,Spanish

File techtable2.txt:

oraclelinux7]$ cat techtable2.txt


ITL,Italy,Italian
ENG,England,English
SWE,Sweden,Swedish
HUN,Hungary,Hungarian

37
© 2020 EPICO TECH
- What can you do with Python?
2 - E.g Comparing files to be used by an Oracle External tables :

2.2 File comparetablefiles.py:

oraclelinux7]$ cat comparetablefiles.py


f1=open("techtable1.txt","r")
f2=open("techtable2.txt","r")
n=0
for line1 in f1:
for line2 in f2:
if line1==line2:
print("Same value in both lines.\n", line1[n])
n += 1
else:
print("The line" ,n, "at the file techtable1.txt is:")
print(line1)
print(" The line", n, "at the file techtable2.txt is:")
print(line2)
n += 1
break
f1.close()
f2.close() 38
© 2020 EPICO TECH
- What can you do with Python?
2 - E.g Comparing files to be used by an Oracle External tables :
2.3 - File comparetablefiles.py:
oraclelinux7]$ python comparetablefiles.py
('The line', 0, 'at the file techtable1.txt is:')
BRZ,Brazil,Portuguese

(' The line', 0, 'at the file techtable2.txt is:')


ITL,Italy,Italian

('The line', 1, 'at the file techtable1.txt is:')


NEZ,New Zealand,English

(' The line', 1, 'at the file techtable2.txt is:')


ENG,England,English

('The line', 2, 'at the file techtable1.txt is:')


PRT,Portugal,Portuguese

(' The line', 2, 'at the file techtable2.txt is:')


SWE,Sweden,Swedish

('The line', 3, 'at the file techtable1.txt is:')


SPN,Spain,Spanish

(' The line', 3, 'at the file techtable2.txt is:')


HUN,Hungary,Hungarian 39
© 2020 EPICO TECH
- What can you do with Python?
4- Python and OCI:
4.1 - Python OCI SDK:
Documentation:https://docs.cloud.oracle.com/en-us/iaas/Content/API/SDKDocs/pythonsdk.htm
Github Oracle: https://github.com/oracle/oci-python-sdk

40
© 2020 EPICO TECH
- What can you do with Python?
4- Python and OCI (Oracle Cloud Infrastructure ):
4.2 - Oracle Application Container Cloud Service :
https://docs.oracle.com/en/cloud/paas/app-container-cloud/index.html

41
© 2020 EPICO TECH
- What can you do with Python?
4- Python and OCI (Oracle Cloud Infrastructure ):
4.2 - Oracle Application Container Cloud Service : Deploy a Python Application to Oracle Cloud

3- Steps:

- Create a Python Application


- Prepare the Application for Deployment
- Deploy Your Application to Oracle Application Container Cloud Service

42
© 2020 EPICO TECH
43
© 2020 EPICO TECH
- Python and Oracle DBA career?
- Oracle Database Administrator L3
role

44
© 2020 EPICO TECH
- Python and Oracle DBA career?
- Oracle DBA role

45
© 2020 EPICO TECH
- Python and Oracle DBA career?
- Oracle Database Administrator

46
© 2020 EPICO TECH
Beginner-friendly Python for Oracle Techies

47
© 2020 EPICO TECH
Beginner-friendly Python for Oracle Techies

© 2020 EPICO TECH


48
QUESTIONS?

E-mail: [email protected]

49
© 2020 EPICO TECH

You might also like