Memory dump: Work with XMLField in Oracle (part 3)

Posted by in Software

TL;DR: Here I present how to insert data with python in the table created at the previous step.

Table of Contents

  1. Part 1 – Prerequisites
  2. Part 2 – Create a table and perform a SELECT
  3. Part 3 – User Python to insert data
  4. Part 4 – references

In the previous entry, I’ve finished by creating the table, loading an entry manually and query it. Everything was done via SQL, so repeating the same process for a large number of XMLs is not really an idea I’d entertain too much. As my language of choice is python, I’ve decided to write a script to scan directories for the relevant files and upload them accordingly.

Preliminary stuff

To make the script more versatile, I’m using argparse:

This will allow to specify a custom starting folder, in case I want to import more/all records later.

The Database Connection

Connecting to the Oracle DB in python is easy if you have all credentials:

I’ve tried before using cx_Oracle.Connection() but it didn’t work. This works for meTM.

Walk the files

My sample structure is:

So, to accommodate even more layers in the future (e.g. a per-year grouping), The code looks like this:

This code will take care of reading the XML files and give me the identifier, article and citations strings.

Insert the XML into Oracle DB

Now that we have the table created in the previous article, we can connect to the DB from python and that we are able to scan the directories and load the files, the next step is to insert the read contents into the DB.

Prepare the cursor

To do this, we need to first create a cursor:

The cursor.setinputsizes() function gives some hints as to the size and type (at some extent) of the data that will be loaded. In our case, we’ll need the first field to be of a length of 20 (characters) and the subsequent fields are of CLOB types (therefore of variable length).

I chose to use the couple cursor.prepare(), cursor.insert(None, data) rather than cursor.insert('sql string', data) command directly, just to have the code clearer. The statement would be the same.

Important! If you’re getting errors trying to insert XMLTYPE fields, the above “xmltype(:var)” is what you’re looking for. by default, the above bound variables would be VARCHAR(20), CLOB and CLOB respectively while we want VARCHAR(20), XMLTYPE and XMLTYPE. Oracle offers a cast-like function.

Loading the data

Once our cursor is prepared, all we need to do is to use it. So, inside the loop, just after the citations processing, insert:

I’ve complicated this a bit to handle duplicated records and output XML parsing issues. The simple variant is:

Once you’re done with the loop close the cursor via cursor.close() and when you’re finished with the whole processing, close the DB connection too ( connection.close())

Conclusion

Loading data from python into Oracle DB is doable for exotic types like XMLTYPE and the combination is very powerful. One can combine these technologies with e.g. django and REST to get very interesting results :)

HTH,


A little experiment: If you find this post and ad below useful, please check the ad out :-)