inserting multiple values - sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type

inserting multiple values - sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type

错误:

cursor.execute("INSERT INTO details (user_id, first_name, surname, role, make, model, colour, reg) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", details_default_values) sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.

导致此错误发生的代码:

connection = sqlite3.connect('collyers_car_park.db')

cursor = connection.cursor()

# create details table
details_table = """CREATE TABLE IF NOT EXISTS
details(
user_id INTEGER PRIMARY KEY,
first_name TEXT,
surname TEXT,
role TEXT,
make TEXT,
model TEXT,
colour TEXT,
reg TEXT)"""

details_default_values = [
    ('1','Bob','Smith','Staff','Lamborgini','Aventador', 'Red', 'RE05 KDJ'),
    ('2','Sarah','McDonald','Staff','Ferrari','LaFerrari', 'Yellow', 'TY07 PER'),
    ('3','Will','Stevenson','Student','Bugatti','Veyron', 'Green', 'RE62 LKD'),
    ('4','Steve','Swimswam','Student','Renault','Clio', 'Pink', 'RE66 KPO'),
    ('5','Harry','Reeto','Visitor','VW','Up!', 'Blue', 'RZ05 FSD'),
    ('5','Harry','Reeto','Visitor','VW','Up!', 'Blue', 'RZ05 FSD'),
    ('5','Harry','Reeto','Visitor','VW','Up!', 'Blue', 'RZ05 FSD'),
    ('5','Harry','Reeto','Visitor','VW','Up!', 'Blue', 'RZ05 FSD'),
]

cursor.execute("INSERT INTO details (user_id, first_name, surname, role, make, model, colour, reg) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", details_default_values)

我认为 user_id 列是字符串格式的问题,但当我去掉它们周围的引号时,我仍然遇到同样的错误。

您正试图插入超过 1 行,因此请使用 cursor.executemany():

而不是 cursor.execute()
cursor.executemany("INSERT INTO ....", details_default_values)
connection.commit()

但是,还有另一个问题,因为您要插入重复的 user_id,这将导致 UNIQUE constraint failed 错误。
确保所有 user_id 都是唯一的。