Cursor write 3rd file empty - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Cursor write 3rd file empty (/thread-36604.html) |
Cursor write 3rd file empty - paulo79 - Mar-09-2022 Hi all, I have this code below where from table all have values so all files generated should not be empty. import cx_Oracle as oracledb def output_type_handler(cursor, name, default_type, size, precision, scale): if default_type == oracledb.CLOB: return cursor.var(oracledb.LONG_STRING, arraysize=cursor.arraysize) if default_type == oracledb.BLOB: return cursor.var(oracledb.LONG_BINARY, arraysize=cursor.arraysize) conn = oracledb.connect("/", mode=oracledb.SYSASM) conn.outputtypehandler = output_type_handler prev_name = None cursor.execute("select cellname, confval from v$cell_config where conftype='CELL' order by cellname desc") for name, val in cursor: if name != prev_name: filename = "my_file_%s.xml" %name print("File name is: ", filename) print("XML Content for this line is: ", val) f = open(filename,"w") prev_name = name f.write(val)It generate 3 files where 1 is empty: If I debug using print I can see data for all 3 lines . Seems cursor is writing X-1 (Skeeping the latest one it tries to generate. It create file but did not write to it.) Can anyone help on this? RE: Cursor write 3rd file empty - deanhystad - Mar-10-2022 Close your files. A few comments about your code. Are there only two possible types? def output_type_handler(cursor, name, default_type, size, precision, scale): if default_type == oracledb.CLOB: return cursor.var(oracledb.LONG_STRING, arraysize=cursor.arraysize) if default_type == oracledb.BLOB: return cursor.var(oracledb.LONG_BINARY, arraysize=cursor.arraysize)If so, you should write like this: def output_type_handler(cursor, name, default_type, size, precision, scale): if default_type == oracledb.CLOB: return cursor.var(oracledb.LONG_STRING, arraysize=cursor.arraysize) return cursor.var(oracledb.LONG_BINARY, arraysize=cursor.arraysize)If there are more types than CLOB and BLOB, you should raise an exception def output_type_handler(cursor, name, default_type, size, precision, scale): if default_type == oracledb.CLOB: return cursor.var(oracledb.LONG_STRING, arraysize=cursor.arraysize) if default_type == oracledb.BLOB: return cursor.var(oracledb.LONG_BINARY, arraysize=cursor.arraysize) raise ValueError("Unsupported output type")Functions should never return a value, always return a value, or throw an error if it is supposed to return something but can't. This prevents the confusing error message: Is this code supposed to prevent duplicates?for name, val in cursor: if name != prev_name:It will if the replay is sorted by name. Is that the case? Is that always the case? And be sure to close your files. for name, val in cursor: if name != prev_name: filename = "my_file_%s.xml" %name print(f"File name is: {filename}\nXML Content for this line is: {val}") with open(filename, "w") as file: # Context manager automatically closes file for you file.write(val) prev_name = name RE: Cursor write 3rd file empty - paulo79 - Mar-10-2022 It is showing errors like below: >>> cursor = conn.cursor() >>> cursor.execute("select cellname, confval from v$cell_config where conftype='CELL' order by cellname desc") <cx_Oracle.Cursor on <cx_Oracle.Connection to user @local>> >>> for name, val in cursor: ... if name != prev_name: ... filename = "my_file_%s.xml" %name ... print(f"File name is: {filename}\nXML Content for this line is: {val}")
I'm using python 2.7. Seems it does not work with print(f) . I remove and now is working fine. tks RE: Cursor write 3rd file empty - DeaD_EyE - Mar-10-2022 (Mar-10-2022, 01:42 PM)paulo79 Wrote: I'm using python 2.7. Seems it does not work with print(f) . I remove and now is working fine. tks from __future__ import print_functionhttps://docs.python.org/3/library/__future__.html Support since Python 2.6.0a2 , but if you import the print_function , you've to change all existing code where print is used.
|