此错误通常是由于在向Oracle数据库中插入NaN、无穷大或超出float64数据类型范围的值时触发的。解决此问题的方法是将这些值转换为有效的数字。以下是一个示例代码,演示如何使用numpy将NaN和无穷大值替换为有效的数字。
import numpy as np
import cx_Oracle
# connect to the Oracle database
connection = cx_Oracle.connect("username/password@hostname:port/service_name")
# define your data array with NaN or infinity values
data = np.array([1.0, 2.0, np.nan, np.inf, -np.inf])
# replace NaN and infinity values with valid numbers
data[np.isnan(data)] = -99999.0
data[data == np.inf] = 99999.0
data[data == -np.inf] = -99999.0
# insert the data into the Oracle database
cursor = connection.cursor()
for value in data:
cursor.execute("INSERT INTO my_table (my_column) VALUES (:value)", value=value)
# commit the changes and close the connections
connection.commit()
cursor.close()
connection.close()