HOW TO

Connect and run SQL queries to an Oracle database from Python

Last Published: April 25, 2020

Summary

Connecting to Oracle databases from Python provides the ability to run essential SQL queries that can be used in geoprocessing tasks.Instructions provided describe how to connect to an Oracle database and run SQL queries from a Python script.

Procedure

  1. Download and install the appropriate cx_Oracle module for the installed Python release (for example, Python 2.6, 2.7, etc.):
  2. Import the module in the Python script:
import cx_Oracle
Note:
Ensure that you have fulfilled the prerequisites described the Overview section of the installation instructions, and have downloaded and installed the Oracle Client Libraries before making a connection to the database.
  1. Make a connection to an Oracle database by passing in the appropriate user/password to the following connection string:
connection = cx_Oracle.connect('sde/sde@orcl')
Note:
If an error is returned containing the message "Error: No module named cx_Oracle", verify the installation of the cx_Oracle module.
  1. Define a parameter to access the cursor method. This parameter connects to the Oracle instance:
cursor = connection.cursor()
  1. Create a query string:
querystring = "select * from Parcels"
  1. Pass the query string to the cursor method:
cursor.execute(querystring)

Article ID:000011659

Software:
  • ArcGIS Desktop 1
  • Legacy Products

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Related Information

Discover more on this topic