【问题标题】:Catch cx_Oracle executemany() batch insert error "expecting number"捕获 cx_Oracle executemany() 批量插入错误“期望数字”
【发布时间】:2021-07-17 20:25:34
【问题描述】:

我有 56 列和几百万行,我使用以下代码将它们插入到我的 Oracle 表中:

import traceback
import sys
try:
    conn = cx_Oracle.connect()
    cursor = conn.cursor()
    df_local = df_scores.astype(object).where(pd.notnull(df_scores), None).copy()
    q_t = '''INSERT INTO Table VALUES ( to_date(  :1, 'yyyy-mm-dd hh24:mi:ss'), '''
    s=''
    for i in range(2, 56):
       s = s + ':'+str(i)+','
    s = s[0:-1] + ' )'
    q_t = q_t + s
    rows = [tuple(x) for x in df_local.values]
    cursor.executemany(q_t, rows)
    conn.commit()
except:    
   print(traceback.print_exc())
   print(sys.exc_info()[2])

在某行我收到TypeError: expecting numbererror。我想知道是否有一种方法可以获得产生错误的确切行数据,否则每次遇到此类错误时都很难调试?使用的堆栈跟踪没有提供任何进一步的细节。

【问题讨论】:

    标签: python oracle cx-oracle


    【解决方案1】:

    如果您要查找哪些行包含无效数据,您可以在executemany() 期间设置batcherrors=True。这将为您提供插入期间失败的所有行的列表。您可以在此处找到一个具有更详细说明的示例:https://cx-oracle.readthedocs.io/en/latest/user_guide/batch_statement.html#handling-data-errors。

    【讨论】:

    • 不幸的是,仍然得到同样的错误:rows = [tuple(x) for x in df_local.values] cursor.executemany(q_t, rows, batcherrors=True) TypeError: expecting number
    猜你喜欢
    • 2019-10-17
    • 2018-08-05
    • 1970-01-01
    • 2021-03-03
    • 2019-05-02
    • 2020-02-29
    • 2023-03-13
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多