读取 lob 时出现 cx_Oracle MemoryError
当尝试使用 cx_Oralce 从 lob 字段读取数据时,我收到“exceptions.MemoryError”。 这段代码一直有效,这个 lob 字段似乎太大了。
Example:
xml_cursor = ora_connection.cursor()
xml_cursor.arraysize = 2000
try:
xml_cursor.execute(“select xml_data from xmlTable where id = 1”)
for row_data in xml_cursor.fetchall():
str_xml = str(row_data[0]) #this throws “exceptions.MemoryError”
When trying to read data from a lob field using cx_Oralce I’m receiving “exceptions.MemoryError”. This code has been working, this one lob field seems to be too big.
Example:
xml_cursor = ora_connection.cursor()
xml_cursor.arraysize = 2000
try:
xml_cursor.execute(“select xml_data from xmlTable where id = 1”)
for row_data in xml_cursor.fetchall():
str_xml = str(row_data[0]) #this throws “exceptions.MemoryError”
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,如果 Python 给出 MemoryError,则意味着该行中的一个字段占用的内存比您拥有的内存多(当然对于 LOB 来说这是可能的)。 您必须将其分割并分成块(反复使用
select dbms_lob.substr(xml_data, ...
) 并将其提供给增量 XML 解析器(或将其写出到文件中) ,或者您尝试使用该多 GB LOB 执行的任何操作)DBMS_LOB
是 Oracle 提供的一个文档齐全的软件包,您可以在很多地方找到它的文档,例如 < a href="http://www.stanford.edu/dept/itss/docs/oracle/10g/appdev.101/b10802/d_lob.htm" rel="noreferrer">此处。Yep, if Python is giving MemoryError it means that just that one field of that just one row takes more memory than you have (quite possible with a LOB of course). You'll have to slice it up and get it in chunks (with
select dbms_lob.substr(xml_data, ...
repeatedly) and feed it to an incremental XML parser (or write it out to a file, or whatever is it that you're trying to do with that multi-GB LOB).DBMS_LOB
is a well-documented Oracle-supplied package, and you can find its docs in many places, e.g. here.