|
1 | | -.. _xmldatatype: |
2 | | - |
3 | | -****************** |
4 | | -Using XMLTYPE Data |
5 | | -****************** |
6 | | - |
7 | | -Oracle XMLType columns are fetched as strings by default. This is currently |
8 | | -limited to the maximum length of a ``VARCHAR2`` column. To return longer XML |
9 | | -values, they must be queried as LOB values instead. |
10 | | - |
11 | | -The examples below demonstrate using XMLType data with python-oracledb. The |
12 | | -following table will be used in these examples: |
13 | | - |
14 | | -.. code-block:: sql |
15 | | -
|
16 | | - CREATE TABLE xml_table ( |
17 | | - id NUMBER, |
18 | | - xml_data SYS.XMLTYPE |
19 | | - ); |
20 | | -
|
21 | | -Inserting into the table can be done by simply binding a string as shown: |
22 | | - |
23 | | -.. code-block:: python |
24 | | -
|
25 | | - xml_data = """<?xml version="1.0"?> |
26 | | - <customer> |
27 | | - <name>John Smith</name> |
28 | | - <Age>43</Age> |
29 | | - <Designation>Professor</Designation> |
30 | | - <Subject>Mathematics</Subject> |
31 | | - </customer>""" |
32 | | - cursor.execute("insert into xml_table values (:id, :xml)", |
33 | | - id=1, xml=xml_data) |
34 | | -
|
35 | | -This approach works with XML strings up to 1 GB in size. For longer strings, a |
36 | | -temporary CLOB must be created using :meth:`Connection.createlob()` and bound |
37 | | -as shown: |
38 | | - |
39 | | -.. code-block:: python |
40 | | -
|
41 | | - clob = connection.createlob(oracledb.DB_TYPE_CLOB) |
42 | | - clob.write(xml_data) |
43 | | - cursor.execute("insert into xml_table values (:id, sys.xmltype(:xml))", |
44 | | - id=2, xml=clob) |
45 | | -
|
46 | | -Fetching XML data can be done simply for values that are shorter than the |
47 | | -length of a VARCHAR2 column as shown: |
48 | | - |
49 | | -.. code-block:: python |
50 | | -
|
51 | | - cursor.execute("select xml_data from xml_table where id = :id", id=1) |
52 | | - xml_data, = cursor.fetchone() |
53 | | - print(xml_data) # will print the string that was originally stored |
54 | | -
|
55 | | -For values that exceed the length of a VARCHAR2 column, a CLOB must be returned |
56 | | -instead by using the function ``XMLTYPE.GETCLOBVAL()`` as shown: |
57 | | - |
58 | | -.. code-block:: python |
59 | | -
|
60 | | - cursor.execute(""" |
61 | | - select xmltype.getclobval(xml_data) |
62 | | - from xml_table |
63 | | - where id = :id""", id=1) |
64 | | - clob, = cursor.fetchone() |
65 | | - print(clob.read()) |
66 | | -
|
67 | | -The LOB that is returned can be streamed or a string can be returned instead of |
68 | | -a CLOB. See :ref:`lobdata` for more information about processing LOBs. |
| 1 | +.. _xmldatatype: |
| 2 | + |
| 3 | +****************** |
| 4 | +Using XMLTYPE Data |
| 5 | +****************** |
| 6 | + |
| 7 | +Oracle XMLType columns are fetched as strings by default. This is currently |
| 8 | +limited to the maximum length of a ``VARCHAR2`` column. To return longer XML |
| 9 | +values, they must be queried as LOB values instead. |
| 10 | + |
| 11 | +The examples below demonstrate using XMLType data with python-oracledb. The |
| 12 | +following table will be used in these examples: |
| 13 | + |
| 14 | +.. code-block:: sql |
| 15 | +
|
| 16 | + CREATE TABLE xml_table ( |
| 17 | + id NUMBER, |
| 18 | + xml_data SYS.XMLTYPE |
| 19 | + ); |
| 20 | +
|
| 21 | +Inserting into the table can be done by simply binding a string as shown: |
| 22 | + |
| 23 | +.. code-block:: python |
| 24 | +
|
| 25 | + xml_data = """<?xml version="1.0"?> |
| 26 | + <customer> |
| 27 | + <name>John Smith</name> |
| 28 | + <Age>43</Age> |
| 29 | + <Designation>Professor</Designation> |
| 30 | + <Subject>Mathematics</Subject> |
| 31 | + </customer>""" |
| 32 | + cursor.execute("insert into xml_table values (:id, :xml)", |
| 33 | + id=1, xml=xml_data) |
| 34 | +
|
| 35 | +This approach works with XML strings up to 1 GB in size. For longer strings, a |
| 36 | +temporary CLOB must be created using :meth:`Connection.createlob()` and bound |
| 37 | +as shown: |
| 38 | + |
| 39 | +.. code-block:: python |
| 40 | +
|
| 41 | + clob = connection.createlob(oracledb.DB_TYPE_CLOB) |
| 42 | + clob.write(xml_data) |
| 43 | + cursor.execute("insert into xml_table values (:id, sys.xmltype(:xml))", |
| 44 | + id=2, xml=clob) |
| 45 | +
|
| 46 | +Fetching XML data can be done simply for values that are shorter than the |
| 47 | +length of a VARCHAR2 column as shown: |
| 48 | + |
| 49 | +.. code-block:: python |
| 50 | +
|
| 51 | + cursor.execute("select xml_data from xml_table where id = :id", id=1) |
| 52 | + xml_data, = cursor.fetchone() |
| 53 | + print(xml_data) # will print the string that was originally stored |
| 54 | +
|
| 55 | +For values that exceed the length of a VARCHAR2 column, a CLOB must be returned |
| 56 | +instead by using the function ``XMLTYPE.GETCLOBVAL()`` as shown: |
| 57 | + |
| 58 | +.. code-block:: python |
| 59 | +
|
| 60 | + cursor.execute(""" |
| 61 | + select xmltype.getclobval(xml_data) |
| 62 | + from xml_table |
| 63 | + where id = :id""", id=1) |
| 64 | + clob, = cursor.fetchone() |
| 65 | + print(clob.read()) |
| 66 | +
|
| 67 | +The LOB that is returned can be streamed or a string can be returned instead of |
| 68 | +a CLOB. See :ref:`lobdata` for more information about processing LOBs. |
0 commit comments