0

This script doesn't produce any error when I ran it but it only insert single item into database. If I do print(imageUrl), the are more than 12 items.

    from aliexpress_api_client import AliExpress
from datetime import date, datetime, timedelta
import mysql.connector

cnx = mysql.connector.connect(user='root', password='Kradz579032!!',
                              host='127.0.0.1',
                              database='aliexpressapidb')
cursor = cnx.cursor()

add_data = ("INSERT INTO producttable "
               "(productId, productTitle, salePrice, originalPrice, imageUrl) "
               "VALUES (%s, %s, %s, %s, %s)")

aliexpress = AliExpress('9420', 'bazaarmaya')
data = aliexpress.get_product_list(['productId', 'productTitle', 'salePrice', 'originalPrice', 'imageUrl'], 'pillow')
for product in data['products']:
    productId = product['productId']
    productTitle = product['productTitle']
    salePrice = product['salePrice']
    originalPrice = product['originalPrice']
    imageUrl = product['imageUrl']


data_insert = (productId, productTitle, salePrice, originalPrice, imageUrl)

cursor.execute(add_data, data_insert)





cnx.commit()

cursor.close()
cnx.close()

1 Answer 1

1

You are not executing for every iteration of your forEach loop.

Try to move the data_insert = (productId, productTitle, salePrice, originalPrice, imageUrl) and cursor.execute(add_data, data_insert) inside the loop like this:

from aliexpress_api_client import AliExpress
from datetime import date, datetime, timedelta
import mysql.connector

cnx = mysql.connector.connect(user='root', password='Kradz579032!!',
                              host='127.0.0.1',
                              database='aliexpressapidb')
cursor = cnx.cursor()

add_data = ("INSERT INTO producttable "
               "(productId, productTitle, salePrice, originalPrice, imageUrl) "
               "VALUES (%s, %s, %s, %s, %s)")

aliexpress = AliExpress('9420', 'bazaarmaya')
data = aliexpress.get_product_list(['productId', 'productTitle', 'salePrice', 'originalPrice', 'imageUrl'], 'pillow')
for product in data['products']:
    productId = product['productId']
    productTitle = product['productTitle']
    salePrice = product['salePrice']
    originalPrice = product['originalPrice']
    imageUrl = product['imageUrl']
    data_insert = (productId, productTitle, salePrice, originalPrice, imageUrl)
    cursor.execute(add_data, data_insert)




cnx.commit()

cursor.close()
cnx.close()

That should fix your problem.

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

Comments

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.