Skip to content

Commit 81bb940

Browse files
Improved documentation.
1 parent a0a548b commit 81bb940

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

doc/src/user_guide/batch_statement.rst

+50
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,53 @@ the maximum size of the strings that will be processed is 20 characters. Since
234234
cx_Oracle allocates memory for each row based on this value, it is best not to
235235
oversize it. The first parameter of ``None`` tells cx_Oracle that its default
236236
processing will be sufficient.
237+
238+
Loading CSV Files into Oracle Database
239+
======================================
240+
241+
The :meth:`Cursor.executemany()` method and `csv module
242+
<https://docs.python.org/3/library/csv.html#module-csv>`__ can be used to
243+
efficiently load CSV (Comma Separated Values) files. For example, consider the
244+
file ``data.csv``::
245+
246+
101,Abel
247+
154,Baker
248+
132,Charlie
249+
199,Delta
250+
. . .
251+
252+
And the schema:
253+
254+
.. code-block:: sql
255+
256+
create table test (id number, name varchar2(25));
257+
258+
Instead of looping through each line of the CSV file and inserting it
259+
individually, you can insert batches of records using
260+
:meth:`Cursor.executemany()`:
261+
262+
.. code-block:: python
263+
264+
import cx_Oracle
265+
import csv
266+
267+
. . .
268+
269+
# Predefine the memory areas to match the table definition
270+
cursor.setinputsizes(None, 25)
271+
272+
# Adjust the batch size to meet your memory and performance requirements
273+
batch_size = 10000
274+
275+
with open('testsp.csv', 'r') as csv_file:
276+
csv_reader = csv.reader(csv_file, delimiter=',')
277+
sql = "insert into test (id,name) values (:1, :2)"
278+
data = []
279+
for line in csv_reader:
280+
data.append((line[0], line[1]))
281+
if len(data) % batch_size == 0:
282+
cursor.executemany(sql, data)
283+
data = []
284+
if data:
285+
cursor.executemany(sql, data)
286+
con.commit()

doc/src/user_guide/connection_handling.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -435,9 +435,9 @@ same (and `increment` equal to zero). the firewall, `resource manager
435435
or user profile `IDLE_TIME
436436
<https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-ABC7AE4D-64A8-4EA9-857D-BEF7300B64C3>`__
437437
should not expire idle sessions. This avoids connection storms which can
438-
decrease throughput. See `About Optimizing Real-World Performance with Static
439-
Connection Pools
440-
<https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-BC09F045-5D80-4AF5-93F5-FEF0531E0E1D>`__,
438+
decrease throughput. See `Guideline for Preventing Connection Storms: Use
439+
Static Pools
440+
<https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-7DFBA826-7CC0-4D16-B19C-31D168069B54>`__,
441441
which contains details about sizing of pools.
442442

443443
Session CallBacks for Setting Pooled Connection State

doc/src/user_guide/sql_execution.rst

+7-5
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ optionally :ref:`overridden <outputtypehandlers>`.
4848
.. IMPORTANT::
4949

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

5555
.. _fetching:
5656

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

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

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

0 commit comments

Comments
 (0)