如何将python cx_Oracle与sp一起使用

2024-06-16 10:00:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用python3.4与oracle(11g)/sqldeveloper交互。 cx峎Oracle真的不能处理sqlPlus语句吗?似乎页面https://sourceforge.net/p/cx-oracle/mailman/message/2932119/是这么说的。在

那么我们如何通过python执行“spool”命令呢?在

代码:

import cx_Oracle
db_conn = cx_Oracle.connect(...)
cursor = db_conn.cursor()
cursor.execute('spool C:\\Users\Administrator\Desktop\mycsv.csv')
...

错误:cx_Oracle.Database错误:ORA-00900:


Tags: httpsmailmandbnet错误页面语句conn
1条回答
网友
1楼 · 发布于 2024-06-16 10:00:04

“spool”命令非常特定于SQL*Plus,在cx\u Oracle或任何其他使用OCI(Oracle调用接口)的应用程序中不可用。不过,你也可以做一些类似的事情,而不会太麻烦。在

您可以从自己的cx类创建连接_甲骨文.连接你自己的Cursor类是从cx派生的_甲骨文.光标它可以执行任何日志记录,并有一个特殊的命令“spool”,可以随意打开和关闭它。像这样:

class Connection(cx_Oracle.Connection):

    def __init__(self, *args, **kwargs):
        self.spoolFile = None
        return super(Connection, self).__init__(*args, **kwargs)

    def cursor(self):
        return Cursor(self)

    def spool(self, fileName):
        self.spoolFile = open(fileName, "w")


class Cursor(cx_Oracle.Cursor):

    def execute(self, statement, args):
        result = super(Cursor, self).execute(statement, args)
        if self.connection.spoolFile is not None:
            self.connection.spoolFile.write("Headers for query\n")
            self.connection.spoolFile.write("use cursor.description")

    def fetchall(self):
        rows = super(Cursor, self).fetchall()
        if self.connection.spoolFile is not None:
            for row in rows:
                self.connection.spoolFile.write("row details")

这会让你知道该怎么做。在

相关问题 更多 >