DPY-3002:不支持“int64”类型的Python值;显然我需要将我的代码与 InputHandler 结合起来,但我在结合它们时遇到了困难

问题描述 投票:0回答:1

我有下面的代码,我使用 cx_oracle 从数据库中提取并从 csv 文件传递变量。变量“unitid”确实有 dtype 是 int64,但我尝试了多次转换但没有成功。

基本上,下面的代码必须根据类似的问题与后面的代码组合起来,但我无法将它们放在一起,

这是我的代码:


df = pd.read_csv(r"C:\Users\Vosoua\Desktop\Ingenco_Offers_csv2.csv") 

df=df.dropna()

unitid=df['UNITSCHEDULEID'].unique()


TNS_Admin = os.environ.get('xxxxx')
oracledb.defaults.config_dir = TNS_Admin
pw = input(f'Enter password: ')
connection = oracledb.connect(user='xxxxx', password= pw, dsn="xxxxxx")

query = """SELECT HOUR, UNITSCHEDULEID, VERSIONID, MINRUNTIME
FROM  int_Stg.xxxxxx
WHERE Hour in ({hour_binds})
AND   UnitScheduleId in ({id_binds})""".format(
  hour_binds=",".join((f":{idx}" for idx, _ in enumerate(hour, 1))),
  id_binds=",".join((f":{idx}" for idx, _ in enumerate(unitid, len(hour) + 1))),
)

res = connection.cursor().execute(query, (*hour, *unitid)).fetchall()

我的第二个代码是

def InConverter(value):
    return int(value)  # or whatever is needed to convert from numpy.int64 to an integer

def InputTypeHandler(cursor, value, num_elements):
    if isinstance(value, numpy.int64):
        return cursor.var(int, arraysize=num_elements, inconverter=InConverter)

cursor().inputtypehandler = InputTypeHandler
python pandas cx-oracle
1个回答
0
投票

此电话:

cursor().inputtypehandler = InputTypeHandler

创建一个光标,设置属性,然后销毁光标!那不是你想要的。您可以直接在连接上设置类型处理程序(如

connection.inputtypehandler = InputTypeHandler
中所示),或者您必须这样做:

cursor = connection.cursor()
cursor.inputtypehandler = InputTypeHandler
cursor.execute(query, binds)
rows = cursor.fetchall()
© www.soinside.com 2019 - 2024. All rights reserved.