我可以将带有浮点数的 Numpy 数组保存到 sqlite3,但不能保存带有整数的数组:
import sqlite3
import numpy as np
db = sqlite3.connect('database.db')
database = db.cursor()
database.execute("CREATE TABLE table_name "
"(round INT, id INT, PRIMARY KEY(round, id))")
row_to_write = np.array([1])
dtype = str(row_to_write.dtype)
if dtype.startswith('float'):
database.execute("ALTER TABLE table_name ADD data FLOAT;")
elif dtype.startswith('int'):
database.execute("ALTER TABLE table_name ADD data INTEGER;")
insert_str = "INSERT INTO table_name (round, id, data) VALUES (0, 0, ?);"
database.execute(insert_str, row_to_write)
结果是:
InterfaceError: Error binding parameter 0 - probably unsupported type.
如果我分配一个浮点数组而不是它的工作原理 row_to_write = np.array([1.1])