Build a Python Application
To build a Python application that accesses a JSON database, you start by configuring your development system to support database access that can take advantage of the high performance features of Autonomous JSON Database. Then, in your application you code database connections and SQL statements to take advantage of these features.
Parent topic: Build an Application
Configure Your Python Development System
To configure your development system so that your Python application can take advantage of the high performance features of a JSON database, you perform these steps.
- Download and install Python.
- Download and install Oracle Instant Client.
- Download and install cx_Oracle.
- Download the client credentials for the database and make them available to Oracle Instant Client.
Download and Install Python
-
Oracle Linux:
Oracle Linux 7 includes Python 2.7, so you simply run this command::
sudo yum -y install oracle-release-el7
-
Other OSes and architectures:
Go to the python.org Downloads page and download and install the latest Python 2.7 or Python 3.5 (or later) version for your OS and architecture.
Download and Install Oracle Instant Client
You need Oracle Instant Client libraries version 19.6 or later.
Download and install the Oracle Instant Client basic package for your system's OS and architecture:
-
Oracle Linux:
Run these commands to download and install the Oracle Instant Client basic package:
sudo yum -y install oracle-release-el7 sudo yum -y install oracle-instantclient19.3-basic
(If you want to see a list of all Instant Client packages, go to
http://yum.oracle.com/repo/OracleLinux/OL7/oracle/instantclient/x86_64/index.html
.) -
Other OSes and architectures:
-
Go to the Oracle Instant Client Downloads page and select the download for your system's OS and architecture.
-
On the download page, accept the Oracle Technology Network License Agreement, download the latest version of the Basic Package, and then install it by following the instructions at the bottom of the download page.
-
Download and Install cx_Oracle
Use Python's pip
package to install cx_Oracle
from PyPI (the Python Package Index).:
-
Oracle Linux:
Run these commands to download the
pip
package and then use it to installcx_Oracle
:sudo yum -y install oracle-release-el7 curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py sudo python get-pip.py python -m pip install cx_Oracle --upgrade
-
Other OSes and architectures:
Run this command:
python -m pip install cx_Oracle --upgrade
Download and Install Client Credentials for the Database
-
Download the zip file containing client credentials for your database to a secure directory on your computer.
This zip file is available for download from the database's Details page in the Oracle Cloud console. If you have an Oracle Cloud user account that permits you to access this page, download the credentials as follows. If you don't have such an account, you need to get the zip file from the administrator of the database, together with the password that was used to protect the zip file.
-
In your web browser, sign in to Oracle Cloud and navigate to the Details page for the JSON database.
-
Click DB Connection.
-
On the Database Connection page click Download.
-
In the Download Wallet dialog, enter a wallet password in the Password field and confirm the password in the Confirm Password field.
The password must be at least 8 characters long and must include at least 1 letter and either 1 numeric character or 1 special character.
-
Click Download to save the client credentials zip file to a secure directory.
-
-
After downloading the zip file, follow these steps:
-
Unzip the client credentials zip file.
-
Edit the
sqlnet.ora
file provided in the client credentials, replacing"?/network/admin"
with the full path of the directory where you unzipped the client credentials; for example, change:(DIRECTORY="?/network/admin")
to:
(DIRECTORY="/users/jdoe/adbcredentials")
-
Create the
TNS_ADMIN
environment variable, setting its value to the full path of the directory where you unzipped the client credentials.
-
Parent topic: Build a Python Application
Code Database Connections and SQL Statements
Follow these guidelines to achieve high performance for your application's connections to the database.
- Use connection pools.
- Use the predefined database service that best matches the operations you will be performing. For information about the predefined database services, see Database Service Names for Autonomous Transaction Processing and Autonomous JSON Database in Using Oracle Autonomous Database Serverless.
For example:
pool = cx_Oracle.SessionPool("appuser",
SampleEnv.GetMainPassword(),
"tp",
events=True,
threaded=True)
This example creates a pool for connections to the tp
database service.
Additional Resources
For detailed information about cx_Oracle, go to the cx_Oracle Documentation page.
For code examples that demonstrate a wide variety of cx_Oracle features, go to the python-cx_Oracle samples folder.
Parent topic: Build a Python Application