Skip to content

Commit

Permalink
Improved documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-tuininga committed Oct 1, 2019
1 parent ee893d3 commit e433c72
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
50 changes: 50 additions & 0 deletions doc/src/user_guide/batch_statement.rst
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,53 @@ the maximum size of the strings that will be processed is 20 characters. Since
cx_Oracle allocates memory for each row based on this value, it is best not to
oversize it. The first parameter of ``None`` tells cx_Oracle that its default
processing will be sufficient.

Loading CSV Files into Oracle Database
======================================

The :meth:`Cursor.executemany()` method and `csv module
<https://docs.python.org/3/library/csv.html#module-csv>`__ can be used to
efficiently load CSV (Comma Separated Values) files. For example, consider the
file ``data.csv``::

101,Abel
154,Baker
132,Charlie
199,Delta
. . .

And the schema:

.. code-block:: sql
create table test (id number, name varchar2(25));
Instead of looping through each line of the CSV file and inserting it
individually, you can insert batches of records using
:meth:`Cursor.executemany()`:

.. code-block:: python
import cx_Oracle
import csv
. . .
# Predefine the memory areas to match the table definition
cursor.setinputsizes(None, 25)
# Adjust the batch size to meet your memory and performance requirements
batch_size = 10000
with open('testsp.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
sql = "insert into test (id,name) values (:1, :2)"
data = []
for line in csv_reader:
data.append((line[0], line[1]))
if len(data) % batch_size == 0:
cursor.executemany(sql, data)
data = []
if data:
cursor.executemany(sql, data)
con.commit()
6 changes: 3 additions & 3 deletions doc/src/user_guide/connection_handling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,9 @@ same (and `increment` equal to zero). the firewall, `resource manager
or user profile `IDLE_TIME
<https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-ABC7AE4D-64A8-4EA9-857D-BEF7300B64C3>`__
should not expire idle sessions. This avoids connection storms which can
decrease throughput. See `About Optimizing Real-World Performance with Static
Connection Pools
<https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-BC09F045-5D80-4AF5-93F5-FEF0531E0E1D>`__,
decrease throughput. See `Guideline for Preventing Connection Storms: Use
Static Pools
<https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-7DFBA826-7CC0-4D16-B19C-31D168069B54>`__,
which contains details about sizing of pools.

Session CallBacks for Setting Pooled Connection State
Expand Down
12 changes: 7 additions & 5 deletions doc/src/user_guide/sql_execution.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ optionally :ref:`overridden <outputtypehandlers>`.
.. IMPORTANT::

Interpolating or concatenating user data with SQL statements, for example
``sql = 'SELECT * FROM mytab WHERE mycol = ' + myvar``, is a security risk
``cur.execute("SELECT * FROM mytab WHERE mycol = '" + myvar + "'")``, is a security risk
and impacts performance. Use :ref:`bind variables <bind>` instead. For
example, ``sql = 'SELECT * FROM mytab WHERE mycol = :mybv``.
example, ``cur.execute("SELECT * FROM mytab WHERE mycol = :mybv", mybv=myvar)``.

.. _fetching:

Expand Down Expand Up @@ -338,9 +338,11 @@ object that is returned by default. Python types can be changed with
Changing Fetched Data Types with Output Type Handlers
-----------------------------------------------------

Sometimes the default conversion from Oracle Database type to Python type must
be changed in order to prevent data loss or to fit the purposes of the Python
application. In such cases, an output type handler can be specified.
Sometimes the default conversion from an Oracle Database type to a Python type
must be changed in order to prevent data loss or to fit the purposes of the
Python application. In such cases, an output type handler can be specified for
queries. Output type handlers do not affect values returned from
:meth:`Cursor.callfunc()` or :meth:`Cursor.callproc()`.

Output type handlers can be specified on the :attr:`connection
<Connection.outputtypehandler>` or on the :attr:`cursor
Expand Down

0 comments on commit e433c72

Please sign in to comment.