Python sqlite3的executemany函数返回ValueError

0 投票
1 回答
869 浏览
提问于 2025-04-18 02:05


我有一段Python代码,这段代码是一个更大循环的一部分,我在这个循环中插入经纬度数据和天气数据,以便将来查看。需要注意的是,我在检查当前这次循环的数据是否是float64类型(因为数据也可能被屏蔽,如果是这样我就想跳过这次)。

values = (lat_db,lon_db,sst_db)
        if type(sst_db) != numpy.float64:
            continue

        c.executemany('INSERT INTO current VALUES(?,?,?)',values)

这个表格是通过以下几行代码创建的:

conn = sqlite3.connect('sst.db')
c = conn.cursor()

# Create the database table.
c.execute('''CREATE TABLE current
             (lat real, lon real, sst real)''')

当我运行我的脚本时,出现了以下错误:

    c.executemany('INSERT INTO current VALUES(?,?,?)',values)
ValueError: parameters are of unsupported type

1 个回答

2

execute 这个函数需要一组参数。

executemany 是用来执行多次操作的,所以它需要一组参数的集合,也就是一组一组的参数:

values = ((lat_db,lon_db,sst_db),)
c.executemany('INSERT INTO current VALUES(?,?,?)',values)

这样做只有在你想要插入多条记录的时候才有意义:

values = ((lat_db1, lon_db1, sst_db1),
          (lat_db2, lon_db2, sst_db2))
c.executemany('INSERT INTO current VALUES(?,?,?)', values)

撰写回答