TypeError:将数据插入数据库时,在字符串格式化过程中未转换所有参数

2024-09-29 21:54:56 发布

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

我试图将数据插入到从API获取的数据库中。我以前使用过这个,没有问题,也看不出有什么变化使它停止工作。除“id”、“乘数”和“价格”外,所有字段均为文本字段,其中第一个字段由postgres处理

我已经确保了格式占位符的数量映射了字段的数量,并且查询应该是正确的,所以我看不出问题出在哪里

对于类似问题的其他答案提到了混合字符串格式,但这里的情况并非如此。

我的代码:

cur.execute("""INSERT INTO "public"."main_page_product" ("id","collection_name","description",
"multiplier","dimension","feat1","feat10","feat2","feat3",
"feat4","feat5","feat6","feat7","feat8","feat9","image_url",
"price","short_name","sku","meta_name")VALUES (nextval('catalogue_product_id_seq'::regclass),%s,%s,
'2.2',%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""",
 (temp1['name'], temp1['short_description'], temp1['dimension'],
 temp1['feat1'], temp1['feat2'], temp1['feat3'], temp1['feat4'],
 temp1['feat5'], temp1['feat6'], temp1['feat7'], temp1['feat8'],
 temp1['feat9'], temp1['feat10'], finurl, temp1['price'],
 temp1['short_name'], temp1['sku'], temp1['meta_title']))

TypeError:在字符串格式化过程中并非所有参数都已转换


Tags: 字符串nameid数量格式descriptionproductshort
1条回答
网友
1楼 · 发布于 2024-09-29 21:54:56

列名和参数之间不匹配:

INSERT INTO "public"."main_page_product" 
("id","collection_name","description","multiplier",    # 4
 "dimension","feat1","feat10","feat2",                 # 4
 "feat3","feat4","feat5","feat6",                      # 4
 "feat7","feat8","feat9","image_url",                  # 4
 "price","short_name","sku","meta_name")               # 4

 VALUES 

(nextval('catalogue_product_id_seq'::regclass),%s,%s,'2.2',  # 4
%s,%s,%s,%s,                                                 # 4
%s,%s,%s,%s,                                                 # 4
%s,%s,%s,%s,                                                 # 4
%s,%s,%s)""",                                                # 3

相关问题 更多 >

    热门问题