TypeError:没有足够的参数用于格式字符串?

2024-09-30 18:23:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我从表格中得到一份清单:

hobby = request.form.getlist('hobby')

该列表类似于:

^{pr2}$

我想将此列表存储在mysql服务器中,因此我尝试了:

cursor.executemany('INSERT into hobby(list,a_id) VALUES(%s,%s)', (hobby, current_user.id))
return '<h1> Inserted </h1>'

Tags: form服务器id列表requestmysqlh1cursor
1条回答
网友
1楼 · 发布于 2024-09-30 18:23:58

^{}接受iterable的iterable作为第二个参数。执行查询时,每个项都将映射到一次。然后每个项都包含一个iterable,其中包含要填充的参数。在

因此,在这种情况下,我们应该这样构建:

cursor.executemany('INSERT into hobby(list,a_id) VALUES(%s,%s)',
                   [(interest, current_user.id) for interest in interests])

N.B.: do not call variables things like list, since you will override the reference to the builtin list class. Here we named the list interests.

相关问题 更多 >