Python TypeError:将数据插入数据库时,在字符串格式化过程中未转换所有参数
我试图将数据插入到从API获取的数据库中。我以前使用过这个,没有问题,也看不出有什么变化使它停止工作。除“id”、“乘数”和“价格”外,所有字段均为文本字段,其中第一个字段由postgres处理 我已经确保了格式占位符的数量映射了字段的数量,并且查询应该是正确的,所以我看不出问题出在哪里 对于类似问题的其他答案提到了混合字符串格式,但这里的情况并非如此。 我的代码:Python TypeError:将数据插入数据库时,在字符串格式化过程中未转换所有参数,python,python-3.x,postgresql,psycopg2,Python,Python 3.x,Postgresql,Psycopg2,我试图将数据插入到从API获取的数据库中。我以前使用过这个,没有问题,也看不出有什么变化使它停止工作。除“id”、“乘数”和“价格”外,所有字段均为文本字段,其中第一个字段由postgres处理 我已经确保了格式占位符的数量映射了字段的数量,并且查询应该是正确的,所以我看不出问题出在哪里 对于类似问题的其他答案提到了混合字符串格式,但这里的情况并非如此。 我的代码: cur.execute("""INSERT INTO "public"."main_page_product" ("id","co
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:并非所有参数都在字符串格式化过程中转换列名和参数之间不匹配:
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
列名和参数之间不匹配:
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
那是一行冗长的代码你能多行吗?@Ethak抱歉,完成:)那是一行很长的代码。:)你能多行吗?@Ethak抱歉,完成:)我觉得自己像个白痴,我至少数了10次,但你的格式让我看得很清楚。谢谢你。@JakeRankin碰巧:)休息一下,呼吸点新鲜空气,喝点咖啡(或者根据你有多累睡一会儿;o)我想拍一张这段代码的照片,把它挂在墙上的某个地方。现在这看起来像是艺术。我觉得自己像个白痴,我肯定已经数到了至少10次,但你的格式让它如此明显。谢谢你。@JakeRankin碰巧:)休息一下,呼吸点新鲜空气,喝点咖啡(或者根据你有多累睡一会儿;o)我想拍一张这段代码的照片,把它挂在墙上的某个地方。这看起来像是艺术。