0

I've printed the output of my "payload" which I want to save to the MySQL database:

('B01MTOV8IP', '40462', '23.95', 'n/a', 'Usually ships in 24 hours', 
'https://www.amazon.com/reviews/iframe?akid=AKIAIDCPAFSAQICDTFNQ&alinkCode=xm2&asin=B01MTOV8IP&atag=reakenture-20&exp=2017-08-25T17%3A27%3A37Z&v=2&sig=3zbBXVo4cQAJueFeVeo%252F%252FejvaUOmvuwAtfB4EfMyDiU%253D', 'CHG-GSTWL')

There seems to be something wrong with the way I am formatting it before I pass it to connect.

try:
    selling_price = product.price_and_currency
    selling_price_v = selling_price[0]#type
    print selling_price_v
except Exception as e:
    selling_price = "n/a"

conn = MySQLdb.connect(host="clabadmin.cfcudy1fdz8o.us-east-1.rds.amazonaws.com", user="", passwd="", db="")
payload =[
asin,
bsr,
str(selling_price_v),
str(listing_price_v),
# availability_type,
availability,
reviews,
sku]
print payload 
# conn = sqlite3.connect('skubsr.db')
c = conn.cursor()
c.execute("""UPDATE webservice_bsr 
SET 
AISN = %s,
Best_Sellers_Rank = %s,
selling_price =  %s,
price = %s,
# availability_type = %s,
availability = %s,
reviews = %s
WHERE ItemSKU = %s""", payload)
conn.commit()

I get the following error:

Traceback (most recent call last):
  File "/home/trackstarz/clabReportScraper/bsrimport.py", line 907, in <module>
    WHERE ItemSKU = %s""", payload)
  File "/usr/local/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 187, in execute
    query = query % tuple([db.literal(item) for item in args])
TypeError: not enough arguments for format string
[Finished in 3.1s with exit code 1]
3
  • Why do you think the hash will do anything in the query? Commented Aug 24, 2017 at 17:35
  • I deleted it and same error Commented Aug 24, 2017 at 17:38
  • You have 8 occurrences of %s in your query, but payload has only 7 elements. A # doesn't comment out part of the query; it would simply produce a syntax error if c.execute actually tried to send it to the database. Commented Aug 24, 2017 at 17:38

2 Answers 2

1

# is only used to indicate a comment when used inside Python code. In your query, it is inside the query string, and so is not parsed as a comment identifier, but as part of the query.

If you delete it, you are left with 8 %s and only 7 items inside payload.

Sign up to request clarification or add additional context in comments.

Comments

0

I believe the problem is you have multiple %s string indicators in your execute string but are only giving it a single item (in this case a list) which it doesn't know it should break down into multiple values.

Try using some of the suggestions in this post to get your desired effect.

Using Python String Formatting with Lists

1 Comment

No, the execute() method knows how to handle the sequence.

Your Answer

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