我正在尝试将数据插入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表?
下面是再现您的问题的最小示例
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']]