【问题标题】:Python cx_oracle insert errorPython cx_oracle 插入错误
【发布时间】:2018-01-23 16:30:30
【问题描述】:

我正在尝试使用python 脚本将来自csv 的记录插入oracle 11g 数据库。

主要是应用程序成功插入了一些记录,但后来抛出此异常 Error <class 'cx_Oracle.DatabaseError'>

def orcl_proc(sql):
    # Open database connection
    db = cx_Oracle.connect('username/password@localhost/XE')
    # prepare a cursor object using cursor() method
    cursor = db.cursor()
    try:
        # Execute the SQL command
        cursor = cursor.execute(sql)
        # Commit your changes in the database
        db.commit()
    except cx_Oracle.DatabaseError as e:
        # Log error as appropriate
        error, = e.args
        print('Error.code =', error.code)
        print('Error.message =', error.message)
        print('Error.offset =', error.offset)
        # Rollback in case there is any error
        db.rollback()
    # disconnect from server
    db.close()
    #print('Closed')

错误:

<class 'cx_Oracle.DatabaseError'>

在 56,367 条记录中,python 应用程序只能插入 180 条记录。任何人都可以帮助我,在此先感谢。

【问题讨论】:

    标签: python oracle oracle11g


    【解决方案1】:

    从您的 Python 脚本中删除异常处理程序。让 Oracle 数据库传播它,您就会知道究竟是什么导致了错误,查看它的 Oracle 错误代码和消息文本。

    然后,如果您不确定这些是什么意思,请返回此处并发布 Oracle 的回应。

    【讨论】:

    • 删除错误处理程序后,我得到:=== cx_Oracle.DatabaseError: ORA-12516: TNS:listener could not find available handler with matching protocol stack
    • 只是为了确保:您删除(或评论了)db.commit()db.close() 之间的每一行
    • def orcl_proc1(sql): db = cx_Oracle.connect('EGIS_DATA/thisis4polaris@localhost/XE') cursor = db.cursor() cursor = cursor.execute(sql) db.commit() db.close()
    • 我认为对于每次插入,oracle 都会打开一个新会话,我不知道会话是否可以关闭
    • 啊哈,没看到 ORA-12516(你编辑了评论吗?没关系)。无论如何:如果可能(即您有所需的权限),请尝试修改 PROCESSES 数据库参数检查其值(以 SYS 身份连接)show parameter processes,然后将其放大。也许您应该请您的 DBA 为您做这件事;否则,请阅读:docs.oracle.com/cd/B28359_01/server.111/b28320/…
    【解决方案2】:

    当与 DB 的连接速率高于 DB 配置处理的速率时,可能会出现间歇性“ORA-12516: TNS:listener could not find available handler with matching protocol stack”。尝试碰撞数据库的“进程”参数。如果您需要这方面的帮助,请参阅免费 PDF http://www.oracle.com/technetwork/topics/php/underground-php-oracle-manual-098250.html中的“配置用于测试的数据库”部分。

    【讨论】:

      【解决方案3】:

      我真的找到了答案。感谢 LittleFoot 和 Christopher Jones。 去掉异常处理程序后得到ORA-12516: TNS:listener could not find available handler with matching protocol stack,我要增加数据库进程和会话。

      alter system set processes=1000 scope=spfile;
      alter system set sessions=2248 scope=spfile;
      startup
      

      它奏效了。 谢谢

      【讨论】:

        猜你喜欢
        • 2017-02-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-17
        • 2018-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多