[Zope-DB] DCOracle2: read CLOB in UTF-16 encoding

Wei-Hao at zope.org Wei-Hao at zope.org
Sun Dec 12 11:13:49 EST 2004


Hi all,

When I tried to read a CLOB field in UTF-16 Unicode encoding from
Oracle database, I got the following error message,

Traceback (most recent call last):
  File "D:\src\DCOracle2\DCOracle2\test_DCOracle2.py", line 10, in ?
    s = clob[0].read(clob[0].length(),1,1000)
dco2.DatabaseError: (0, '')
OCI_ERROR at \src\DCOracle2\src\dco2.c:1792: ORA-03127: no new operations
allowed until the active operation ends

1000 is the constant value of OCI_UTF16ID as described in Chapter 16
of Oracle Call Interface Programmer's Guide, Release 9.0.1

After doubling the size of the buffer, I can read CLOB in UTF-16
without problem.  Here is the diff,

Environment: 
Python 2.4
Oracle client: 9.2.0.1.0 on Windows XP
Oracle server: 9.2.0.5.0 on Windows Server 2003
Compiler: Microsoft Visual C++ .Net 2003

Index: dco2.c
===================================================================
RCS file: /cvs-repository/Products/DCOracle2/src/dco2.c,v
retrieving revision 1.137
diff -c -r1.137 dco2.c
*** dco2.c	25 Feb 2004 23:10:49 -0000	1.137
--- dco2.c	12 Dec 2004 07:39:03 -0000
***************
*** 5193,5198 ****
--- 5193,5202 ----
  	if (length < 0 || length > loblength - offset + 1) 
  		length = loblength - offset + 1;
  
+ 	/* If the caller wants to read CLOB in UTF16, we have to 
+ 	prepare larger (double) buffer */ 
+ 	if (csid == OCI_UTF16ID) length *= 2;
+ 
  	string = PyString_FromStringAndSize(NULL, length);
  	if (string == NULL) return NULL;
  	
***************
*** 5216,5221 ****
--- 5220,5231 ----
  
  	if (status != OCI_SUCCESS)
  		return RaiseOCIError(self->errhp, OCI_HTYPE_ERROR);
+ 
+ 	/* return unicode string if the caller requests so */
+ 	if (csid == OCI_UTF16ID) {
+ 		PyObject *unicode_string = PyUnicode_FromEncodedObject(string, "utf16", NULL);
+ 		return unicode_string;
+ 	}
  
  	TRACE(T_EXIT,("sA", "LobLocator_read", string));



More information about the Zope-DB mailing list