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
  Mysql and mysql.connector error lostintime 2 612 Oct-03-2023, 10:25 PM
Last Post: lostintime
  How to detect abnormal data in big database python vanphuht91 5 1,064 Jun-27-2023, 11:22 PM
Last Post: Skaperen
  Database that can compress a column, or all data, automatically? Calab 3 1,120 May-22-2023, 03:25 AM
Last Post: Calab
  Basic SQL query using Py: Inserting or querying sqlite3 database not returning data marlonbown 3 1,306 Nov-08-2022, 07:16 PM
Last Post: marlonbown
  Need help formatting dataframe data before saving to CSV cubangt 16 5,531 Jul-01-2022, 12:54 PM
Last Post: cubangt
  Mysql error message: Lost connection to MySQL server during query tomtom 6 15,685 Feb-09-2022, 09:55 AM
Last Post: ibreeden
  I need help parsing through data and creating a database using beautiful soup username369 1 1,688 Sep-22-2021, 08:45 PM
Last Post: Larz60+
  mySQL Database error not resolving. cybertooth 2 3,119 Aug-30-2021, 05:45 PM
Last Post: ibreeden
  SaltStack: MySQL returner save less data into Database table columns xtc14 2 2,115 Jul-02-2021, 02:19 PM
Last Post: xtc14
  Reading data from mysql. stsxbel 2 2,164 May-23-2021, 06:56 PM
Last Post: stsxbel

Forum Jump:

User Panel Messages

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