【发布时间】:2021-05-29 02:30:02
【问题描述】:
我必须拆开这个大清单并将每一项都放入数据库中。 除了这些数据之外,一切都运行良好。
我试过了:
([styles], [seasons], [day], [id]) = search_
但我收到这样的错误:
exception=ValueError('too many values to unpack (expected 4)')>
我使用这样的查询:
insert= "INSERT INTO cantina (styles,seasons,day,id) VALUES (?,?,?,?)"
db.execute(insert,(styles,seasons,day,id))
我知道我犯了一个错误,但是这里每行有 4 个字段,似乎是正确的
search_ = [[["Redwine"], ['04'], ['01'], ['81535']],
[["Redwine"], ['04'], ['02'], ['81536']],
[["Redwine"], ['04'], ['03'], ['81537']],
[["Redwine"], ['04'], ['04'], ['81538']],
[["Redwine"], ['04'], ['05'], ['81539']],
[["Redwine"], ['04'], ['06'], ['81540']],
[["Redwine"], ['04'], ['07'], ['81541']],
[["Redwine"], ['04'], ['08'], ['81542']],
[["Redwine"], ['04'], ['09'], ['81543']],
[["Redwine"], ['04'], ['10'], ['81544']],
[["Redwine"], ['04'], ['11'], ['81545']],
[["Redwine"], ['04'], ['12'], ['81546']],
[["Redwine"], ['02'], ['01'], ['97179']],
[["Redwine"], ['02'], ['02'], ['97180']],
[["Redwine"], ['02'], ['03'], ['97181']],
[["Redwine"], ['02'], ['04'], ['97182']],
[["Redwine"], ['02'], ['05'], ['97183']],
[["Redwine"], ['02'], ['06'], ['97184']],
[["Redwine"], ['02'], ['07'], ['97185']],
[["Redwine"], ['02'], ['08'], ['97186']],
[["Redwine"], ['02'], ['09'], ['97187']],
[["Redwine"], ['02'], ['10'], ['97188']],
[["Redwine"], ['02'], ['11'], ['97189']],
[["Redwine"], ['02'], ['12'], ['97190']]]
([styles], [seasons], [day], [id]) = search_
insert= "INSERT INTO cantina (styles,seasons,day,id) VALUES (?,?,?,?)"
db.execute(insert,(styles,seasons,day,id))
编辑:
嗨,Barmar,因为该列表来自我加入的另一个函数 四个列表:
def cantina ( cantina_id)
for ... in ...:
styles =
season =
day =
id =
result.append([[styles], [season], [day], [id]]])
return result
而'append'只接受一个参数。
我必须根据“cantina_id”处理一些数据; 将其添加到四个列表并保存到 db
【问题讨论】:
-
为什么每个值都在另一个列表中,即为什么是
["Redwine"]而不是"Redwine"?如果你不这样做,你可以使用executemany()。 -
嗨,Barmar,谢谢,请参阅编辑
-
没有理由像在
append调用中那样将每个值放在自己的列表中。只是不要这样做并使用executemany。 -
你应该使用
result.append([styles, season, day, id])