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
[Link]
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
[Link]
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?
- [Link]
© 2020 EPICO TECH
- What can you do with Python?
- Install in Oracle Linux.
- Documentation: [Link]
© 2020 EPICO TECH
- What can you do with Python?
- Install in Oracle Linux 6
- Documentation: [Link]
27
© 2020 EPICO TECH
- What can you do with Python?
- Install in Oracle Linux 7
- Documentation: [Link]
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: [Link]
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: [Link].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 [Link] 0:9.0.3-7.el7_8 will be installed
---> Package [Link] 0:39.2.0-10.el7 will be installed
...
...
Dependency Installed:
python3-libs.x86_64 0:3.6.8-13.0.1.el7 [Link] 0:9.0.3-7.el7_8 [Link]
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: [Link]
oraclelinux7] python3 -m pip install cx_Oracle --upgrade
Collecting cx_Oracle
Using cached
[Link]
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
[Link]
Oracle Instant Client RPMs
yum install oracle-instantclient12.1-basic-[Link].0-1.x86_64.rpm
(Example of Instant Client Installation for Oracle Database 12cR1):
oraclelinux7]# yum install oracle-instantclient12.1-basic-[Link].0-1.x86_64.rpm
Loaded plugins: langpacks, ulninfo
Examining oracle-instantclient12.1-basic-[Link].0-1.x86_64.rpm: oracle-instantclient12.1-basic-[Link].0-1.x86_64
Marking oracle-instantclient12.1-basic-[Link].0-1.x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package oracle-instantclient12.1-basic.x86_64 0:[Link].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/[Link].d/oracle-
[Link]"
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 [Link].0 Production on Mon Jun 1 [Link] 2020
Copyright (c) 1982, 2014, Oracle. All rights reserved.
Connected to:
Oracle Database 12c Enterprise Edition Release [Link].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 [Link]):
oraclelinux7]$ cat [Link]
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", "[Link]/[Link]")
cursor = [Link]()
[Link](
'SELECT count(*) from all_users')
for value in cursor:
print("Values:", value)
oraclelinux7]$ python3 [Link]
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 [Link]:
oraclelinux7]$ cat [Link]
BRZ,Brazil,Portuguese
NEZ,New Zealand,English
PRT,Portugal,Portuguese
SPN,Spain,Spanish
File [Link]:
oraclelinux7]$ cat [Link]
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 [Link]:
oraclelinux7]$ cat [Link]
f1=open("[Link]","r")
f2=open("[Link]","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 [Link] is:")
print(line1)
print(" The line", n, "at the file [Link] is:")
print(line2)
n += 1
break
[Link]()
[Link]() 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 [Link]:
oraclelinux7]$ python [Link]
('The line', 0, 'at the file [Link] is:')
BRZ,Brazil,Portuguese
(' The line', 0, 'at the file [Link] is:')
ITL,Italy,Italian
('The line', 1, 'at the file [Link] is:')
NEZ,New Zealand,English
(' The line', 1, 'at the file [Link] is:')
ENG,England,English
('The line', 2, 'at the file [Link] is:')
PRT,Portugal,Portuguese
(' The line', 2, 'at the file [Link] is:')
SWE,Sweden,Swedish
('The line', 3, 'at the file [Link] is:')
SPN,Spain,Spanish
(' The line', 3, 'at the file [Link] is:')
HUN,Hungary,Hungarian 39
© 2020 EPICO TECH
- What can you do with Python?
4- Python and OCI:
4.1 - Python OCI SDK:
Documentation:[Link]
Github Oracle: [Link]
40
© 2020 EPICO TECH
- What can you do with Python?
4- Python and OCI (Oracle Cloud Infrastructure ):
4.2 - Oracle Application Container Cloud Service :
[Link]
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: brunorsreis@[Link]
49
© 2020 EPICO TECH