简体   繁体   English

捕获 cx_Oracle executemany() 批量插入错误“期望数字”

[英]Catch cx_Oracle executemany() batch insert error "expecting number"

I have 56 columns and some million rows, which I am inserting in my Oracle Table with the following code:我有 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])

at some row I am getting the TypeError: expecting number error.在某行我收到TypeError: expecting number错误。 I would like to know if there is a way I can get the exact line of data which is generating the error coz otherwise, it would be very difficult to debug every time I get such error?我想知道是否有一种方法可以获得产生错误的确切数据行,否则每次遇到此类错误时都很难调试? The used stack trace does not give any further details.使用的堆栈跟踪没有提供任何进一步的细节。

If you are looking for which rows contain invalid data, you can set batcherrors=True during an executemany() .如果您正在查找哪些行包含无效数据,您可以在executemany()期间设置batcherrors=True This will give you a list of all rows that failed during the insert.这将为您提供插入期间失败的所有行的列表。 You can find an example with a more detailed explanation here: https://cx-oracle.readthedocs.io/en/latest/user_guide/batch_statement.html#handling-data-errors .您可以在此处找到一个具有更详细说明的示例: https://cx-oracle.readthedocs.io/en/latest/user_guide/batch_statement.html#handling-data-errors

I had this issue.我有这个问题。 The src csv had mixed dtypes and no header. src csv 有混合 dtypes,没有 header。

pd.read_csv(datafile, error_bad_lines=True, sep='|', encoding='utf-8-sig', dtype=str, header=None,iterator=True, chunksize=200000): pd.read_csv(数据文件,error_bad_lines=True,sep='|',encoding='utf-8-sig',dtype=str,header=None,iterator=True,chunksize=200000):

--assigned col names via dataframe.columns -- 通过 dataframe.columns 分配列名

df.columns = ['ID','Col2',...] df.columns = ['ID','Col2',...]

--then cast astype(str) to all columns --然后将 astype(str) 强制转换为所有列

for col in df.columns: df[col] = df[col].astype(str).str.encode('ascii', 'replace').str.decode('ascii')对于 df.columns 中的 col: df[col] = df[col].astype(str).str.encode('ascii', 'replace').str.decode('ascii')

worked for me.为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM