Wtforms- 将字段列表数据写入 mysql
Wtforms- writing fieldlist data into mysql
假设我有这样的表格:
Toys= FieldList(TextField('what toys do you have?'),min_entries=5)
Fruits= FieldList(TextField('what fruits do you have?'),min_entries=5)
Jewelry= FieldList(TextField('what jewelries do you have?'),min_entries=5)
Candy= FieldList(TextField('what candies do you have?'),min_entries=5)
没有明确定义提交的值,例如
Toy1=form.Toys[1].data
,我如何聚合表单提交中的所有 FieldList 数据以写入 SQL table,如下所示:?
Toys | Fruits | Jewelry | Candy
ball apple Necklace M&Ms
stick orange Bracelet skittles
top grapes tie clip lollipop
为了简单起见,我为所有字段设置了 min_entries=5
。我尝试使用 for 循环将列名和值附加到字典中并将其写入我的数据库,如下所示:
field_dict=dict()
for f in form:
if str(f.type) in ['FieldList']:
for x in range(1,5):
field_dict.update({f.name:f.data[x]})
sql_insert="INSERT INTO tablename ({}) values ({})".format(
",".join(field_dict.keys()),
",".join('"' + str(value) + '"' for value in field_dict.values()))
c.execute(sql_insert)
但是,它每次遇到 Fieldlist
时都会写入数据库,导致 table 如:
Toys | Fruits | Jewelry | Candy
ball NULL NULL NULL
ball apple NULL NULL
ball apple Necklace NULL
ball apple Necklace M&Ms
我对这个问题的理解是,您有一个表单中的条目列表,您希望将其组合成一个元组并插入到数据库中。
为了节省我的打字时间,假设您有:
# some list of keys obtained or hard coded as global..
KEYS = ['Toys', 'Fruits', 'Candy']
# and a form in which you have
Toys= FieldList(TextField('what toys do you have?'),min_entries=3)
Fruits= FieldList(TextField('what fruits do you have?'),min_entries=3)
Candy= FieldList(TextField('what candies do you have?'),min_entries=3)
现在你的工作是循环表单并打包一个元组:
form = Form()
for _i in range(3):
tup = ()
for key in KEYS:
tup += (getattr(form, key)[_i].data,)
sql = "INSERT INTO tablename ({}) values {}".format(
",".join(KEYS), str(tup))
c.execute(sql)
试一试(或一些经过调试的衍生产品!)
假设我有这样的表格:
Toys= FieldList(TextField('what toys do you have?'),min_entries=5)
Fruits= FieldList(TextField('what fruits do you have?'),min_entries=5)
Jewelry= FieldList(TextField('what jewelries do you have?'),min_entries=5)
Candy= FieldList(TextField('what candies do you have?'),min_entries=5)
没有明确定义提交的值,例如
Toy1=form.Toys[1].data
,我如何聚合表单提交中的所有 FieldList 数据以写入 SQL table,如下所示:?
Toys | Fruits | Jewelry | Candy
ball apple Necklace M&Ms
stick orange Bracelet skittles
top grapes tie clip lollipop
为了简单起见,我为所有字段设置了 min_entries=5
。我尝试使用 for 循环将列名和值附加到字典中并将其写入我的数据库,如下所示:
field_dict=dict()
for f in form:
if str(f.type) in ['FieldList']:
for x in range(1,5):
field_dict.update({f.name:f.data[x]})
sql_insert="INSERT INTO tablename ({}) values ({})".format(
",".join(field_dict.keys()),
",".join('"' + str(value) + '"' for value in field_dict.values()))
c.execute(sql_insert)
但是,它每次遇到 Fieldlist
时都会写入数据库,导致 table 如:
Toys | Fruits | Jewelry | Candy
ball NULL NULL NULL
ball apple NULL NULL
ball apple Necklace NULL
ball apple Necklace M&Ms
我对这个问题的理解是,您有一个表单中的条目列表,您希望将其组合成一个元组并插入到数据库中。
为了节省我的打字时间,假设您有:
# some list of keys obtained or hard coded as global..
KEYS = ['Toys', 'Fruits', 'Candy']
# and a form in which you have
Toys= FieldList(TextField('what toys do you have?'),min_entries=3)
Fruits= FieldList(TextField('what fruits do you have?'),min_entries=3)
Candy= FieldList(TextField('what candies do you have?'),min_entries=3)
现在你的工作是循环表单并打包一个元组:
form = Form()
for _i in range(3):
tup = ()
for key in KEYS:
tup += (getattr(form, key)[_i].data,)
sql = "INSERT INTO tablename ({}) values {}".format(
",".join(KEYS), str(tup))
c.execute(sql)
试一试(或一些经过调试的衍生产品!)