cx_Oracle
is a third-party Python library that facilitates Oracle-to-Python database communication. A cursor is a control structure that enables traversal over the records in a database. This is Python's primary means of accessing database table data. The setup is:
Python interface to Oracle Database conforming to the Python DB API 2.0 specification. oracle/python-cxOracle. The following are 30 code examples for showing how to use cxOracle.connect.These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. This script will guide you to use cxOracle module to retrieve the data from a table. This script will guide you to use cxOracle module to retrieve the data from a table. Summary: in this tutorial, you will learn how to select data from Oracle Database using fetchone, fetchmany, and fetchall methods. To select data from the Oracle Database in a Python program, you follow these steps: First, establish a connection to the Oracle Database using the cxOracle.connect method. CxOracle 8.2, the extremely popular Oracle Database interface for Python, is now production on PyPI. CxOracle is an open source package for the Python Database API specification with many additions to support advanced Oracle Database features.
- Create a connection object
- Define a cursor and call the
cursor()
method on the connection object - Construct a query string for the data of interest
- Pass the query string to the cursor's
execute
method - Step through the cursor as if it were any Python iterable
Here's an example:
The result will be a list of tuples, so data elements can be accessed by row or selectively by referencing record components using index offset:
Note that the returned cursor object is a Python iterator, which means the data elements can only be traversed once.
Per the Python DB API 2.0, details specific to the returned dataset should be written to the cursor's description
attribute. cursor.description
is a list of 7-tuples, each containing information describing the characteristics of a single column:
- fieldname
- datatype
- display_size
- internal_size
- precision
- scale
- null_ok
To obtain the header list for the returned dataset, extract the first element of each 7-tuple in cursor.description
. Here are two ways to go about it (in both cases, headers
will contain the dataset's fieldnames):
Method 1: Extracts the 0-th element from each 7-tuple and appends it to headers
:
Method 2: list comprehension implementation:
Cx_oracle Python Documentation
Cx Oracle Python Executemany Example
Oracle-Python Data Handling and Manipulation Examples
The following are examples of how to carry out common tasks across the Oracle-Python interface.
Query SCHEMA.TABLENAME@DATABASE
, and redirect returned data to a list for additional processing:
Query SCHEMA.TABLENAME@DATABASE
, and redirect output to file usingPython's csv module:
Cx_oracle Python Tutorial
Changing bind variables iteratively. Assume we want to return a separate list of tuples for BLD_FIRE, BLD_WATR and BLD_WTHR perils (each identified in the PERIL_ID field). We'll create a master list, which will contain 3 lists of each associated peril's records. Result will be a list of lists of records: