Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New to the CF scene
    Join Date
    Jun 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do you save api calls into mysql database ?

    I'm trying to save an API call into mysql database but something wrong with my code not sure what it is.

    Code:
    from aliexpress_api_client import AliExpress
    import pymysql.cursors
    
    aliexpress = AliExpress('9420', 'bazaarmaya')
    
    data = aliexpress.get_product_list(['productId', 'productTitle', 'salePrice', 'originalPrice'], 'drones')
    
    #print(data)
    
    connection = pymysql.connect(host='localhost',
                                 user='root',
                                 password='~~~~~~~!!',
                                 db='aliexpressapidb',
                                 charset='utf8mb4',
                                 cursorclass=pymysql.cursors.DictCursor)
    try:
        with connection.cursor() as cursor:
            sql_template ="""
            INSERT INTO producttable (productId, productTitle, salePrice, originalPrice )
                SELECT * FROM (SELECT %(productId)s, %(productTitle)s, %(salePrice)s, %(originalPrice)s) AS tmp
                WHERE NOT EXISTS (
                    SELECT productId FROM producttable WHERE productId = %(productId)s
                )
                LIMIT 1;
            """
    
            for product in data:
                print('%s %s %s %s' % (product['productId'], product['productTitle'], product['salePrice'], product['originalPrice']))
    
                cursor.execute(sql_template, {product['productId'], product['productTitle'],
                                              product['salePrice'], product['originalPrice']})
    
    
            connection.commit()
    
    finally:
        connection.close()
    Error:
    /Users/reezalaq/PycharmProjects/Aliexpress/venv/bin/python /Users/reezalaq/Downloads/newali/script.py
    Traceback (most recent call last):
    File "/Users/reezalaq/Downloads/newali/script.py", line 28, in <module>
    print('%s %s %s %s' % (product['productId'], product['productTitle'], product['salePrice'], product['originalPrice']))
    TypeError: string indices must be integers

    Process finished with exit code 1
    Last edited by firepages; Jun 6th, 2018 at 03:08 PM.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •