Python Forum
Need help saving data into MySQL database
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help saving data into MySQL database
#1
I'm trying to save API data into MySQL database but not sure why this isn't working Confused :

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='Kradz579032!!',
                             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 :

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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  SaltStack: MySQL returner save less data into Database table columns xtc14 2 237 Jul-02-2021, 02:19 PM
Last Post: xtc14
  Reading data from mysql. stsxbel 2 396 May-23-2021, 06:56 PM
Last Post: stsxbel
  Problem updating value in MySQL database dangermaus33 1 367 Nov-24-2020, 08:32 PM
Last Post: dangermaus33
  chatterbot utf-8 errors with mysql database isolatedastronaut 0 413 Nov-08-2020, 06:54 AM
Last Post: isolatedastronaut
  Looping to read data in database CEC68 1 510 Sep-24-2020, 08:54 PM
Last Post: scidam
  saving data from text file to CSV file in python having delimiter as space K11 1 587 Sep-11-2020, 06:28 AM
Last Post: bowlofred
  Get database used data space from pyodbc susja 1 545 Aug-14-2020, 02:01 PM
Last Post: susja
  sqlite3 database does not save data across restarting the program SheeppOSU 1 880 Jul-24-2020, 05:53 AM
Last Post: SheeppOSU
  Importing data from a text file into an SQLite database with Python macieju1974 7 1,181 Jun-29-2020, 08:51 PM
Last Post: buran
  Save Arduino data in mysql server via raspberrypi rithikvg 1 1,019 Jun-24-2020, 10:59 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020