【问题标题】:inserting multiple values - sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type插入多个值 - sqlite3.InterfaceError: Error binding parameter 0 - 可能不支持的类型
【发布时间】:2021-05-07 18:25:20
【问题描述】:

错误:

cursor.execute("INSERT INTO details (user_id, first_name, surname, 角色,品牌,型号,颜色,注册)值(?,?,?,?,?,?,?,?)“, details_default_values) sqlite3.InterfaceError: 绑定错误 参数 0 - 可能是不受支持的类型。

导致此错误发生的代码:

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 列是字符串格式存在问题,但是当我去掉它们周围的引号时,我仍然遇到同样的错误。

【问题讨论】:

  • this 帖子可能对您有所帮助

标签: python database list sqlite sql-insert


【解决方案1】:

您尝试插入多于 1 行,因此请使用 cursor.executemany() 代替 cursor.executemany()

cursor.executemany("INSERT INTO ....", details_default_values)
connection.commit()

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

【讨论】:

  • 谢谢,效果很好。重复值只是为了绕过另一个现在似乎已经消失的错误。
  • .commit() 是做什么的?
  • commit() 保存您对数据库所做的所有更改,并使它们对数据库的其他连接可见。你可以在这里阅读更多:docs.python.org/3/library/sqlite3.html
猜你喜欢
  • 1970-01-01
  • 2013-11-17
  • 1970-01-01
  • 2021-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-24
相关资源
最近更新 更多