2024-09-30 18:23:58 发布
网友
我从表格中得到一份清单:
hobby = request.form.getlist('hobby')
该列表类似于:
我想将此列表存储在mysql服务器中,因此我尝试了:
mysql
cursor.executemany('INSERT into hobby(list,a_id) VALUES(%s,%s)', (hobby, current_user.id)) return '<h1> Inserted </h1>'
^{}接受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.
list
interests
^{} 接受iterable的iterable作为第二个参数。执行查询时,每个项都将映射到一次。然后每个项都包含一个iterable,其中包含要填充的参数。在
因此,在这种情况下,我们应该这样构建:
相关问题 更多 >
编程相关推荐