我正在嘗試將資料插入 oracle 表中,但在此程序中出現此錯誤:
cx_Oracle.NotSupportedError: Python value of type NAType not supported
我的腳本:
data = data_df.values.tolist()
sql = "insert into %s(%s) values(%s)" %(table_name, cols, values)
cursor.executemany(sql, data)
我已經嘗試了 cx-Oracle 檔案(https://cx-oracle.readthedocs.io/en/7.2.3/user_guide/sql_execution.html#inserting-nulls)給出的解決方案。但它回傳了這個錯誤:
cx_Oracle.DatabaseError: ORA-04043: object SDO_GEOMETRY does not exist
因為是在生產環境中,所以我不能對oracle的設定做任何事情。有什么解決方案可以將空值插入到oracle表中嗎?
uj5u.com熱心網友回復:
這是重現您的問題的最小示例
d = {'id': [1, pd.NA], 'col': [ pd.NA,'x' ]}
df = pd.DataFrame(d)
print(df.values.tolist())
cur.executemany("insert into tab(id, col) values (:1, :2)", df.values.tolist())
[[1, <NA>], [<NA>, 'x']]
...
cx_Oracle.NotSupportedError: Python value of type NAType not supported.
注意表定義如下
create table tab(id int, col varchar2(10));
在VARCHAR2列中插入 NA 時出現錯誤 - 是否是數字列,您會觀察到
TypeError: expecting number
解決方案是洗掉 NA并替換它們None
df.loc[0,'col'] = None
df.loc[1,'id'] = None
資料現在如下所示并且insert作業正常
[[1, None], [None, 'x']]
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/333132.html
上一篇:LNNVL替換NOTIN?
