nameerror:pymysql中未定义名称“productid”

nukf8bse  于 2021-06-23  发布在  Mysql
关注(0)|答案(0)|浏览(252)

可能会想办法解决这个问题。我需要通过pymysql将数据保存到数据库这个脚本给了我错误:
“cursor.execute(sql\模板,{“productid”:productid,“producttitle”:producttitle,“saleprice”:saleprice,nameerror:未定义名称'productid'”

import pymysql.cursors

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['products']:
                print('%s %s %s %s' % (
                product['productId'], product['productTitle'], product['salePrice'], product['originalPrice']))

                cursor.execute(sql_template, {"productId": productId, "productTitle": productTitle, "salePrice": salePrice,
                                              "originalPrice": originalPrice})
            connection.commit()

    finally:
        connection.close()

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题