TypeError:将数据插入db

时间:2018-12-23 22:14:24

标签: python python-3.x postgresql psycopg2

我正在尝试将数据插入从API获得的数据库中。我以前没有问题使用过它,看不到发生了什么变化以使其停止工作。除“ id”,“ multiplier”和“ price”外,所有字段均为文本字段,其中第一个由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:在字符串格式化期间并非所有参数都已转换

1 个答案:

答案 0 :(得分:3)

列名和参数之间不匹配:

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