How To: Connect and run SQL queries to an Oracle database from Python
Connecting to Oracle databases from Python provides the ability to run essential SQL queries that can be utilized in geoprocessing tasks.
Instructions provided describe how to connect to an Oracle database and run SQL queries from a Python script.
- Download and install the appropriate cx_Oracle module for the installed Python release (for example, Python 2.6, 2.7, etc.):
- Import the module in the Python script:
- 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')
- Define a parameter to access the cursor method:
cursor = connection.cursor()
- Create a query string:
querystring = "select * from Parcels"
- Pass the query string to the cursor method:
Article ID: 000011659
Software: ArcGIS - ArcEditor 10 ArcGIS - ArcInfo 10 ArcGIS - ArcView 10 ArcGIS for Desktop Advanced 10.1 ArcGIS for Desktop Basic 10.1 ArcGIS for Desktop Standard 10.1 ArcSDE 10, 10.1