0

I'm trying to insert data into a db that is obtained from an API. I have used this previously without issue, and can't see what has changed for it to stop working. All fields are text fields except 'id', 'multiplier' and 'price', the first of which is handled by postgres.

I've ensured the number of format placeholders map the number of fields and that the query should be correct, so I can't see what the issue is.

Other answers to similar problems mentioned mixing string formatting, which is not the case here.

My code:

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: not all arguments converted during string formatting

2
  • That is a looooooooooooooong line of code. :) Can you please multiline that? Commented Dec 23, 2018 at 22:17
  • @EthanK Sorry, done :) Commented Dec 23, 2018 at 22:20

1 Answer 1

3

You have a mismatch between column-names and params:

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
Sign up to request clarification or add additional context in comments.

3 Comments

I feel like an idiot, I must have counted at least 10 times, and yet your formatting makes it so obvious. Thank you.
@JakeRankin happens :) take a break, get some fresh air and some coffee (or some sleep depending on how tired you are ;o)
i want to take a picture of this code and hang it on a wall somewhere. This looks like art now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.