Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 名称错误:名称';productId';未在pymysql中定义_Python_Mysql_Pymysql - Fatal编程技术网

Python 名称错误:名称';productId';未在pymysql中定义

Python 名称错误:名称';productId';未在pymysql中定义,python,mysql,pymysql,Python,Mysql,Pymysql,我想知道如何解决这个问题。我需要通过Pymysql将数据保存到数据库此脚本给了我错误: cursor.execute(sql_模板,{“productId”:productId,“productTitle”:productTitle,“salePrice”:salePrice, NameError:未定义名称“productId” 你如何创建这个“cursor.executemany(sql_模板,数据[“products”])”?@完全不知道你在说什么,因为我是Python初学者。你能告诉我如

我想知道如何解决这个问题。我需要通过Pymysql将数据保存到数据库此脚本给了我错误:

cursor.execute(sql_模板,{“productId”:productId,“productTitle”:productTitle,“salePrice”:salePrice, NameError:未定义名称“productId”


你如何创建这个“cursor.executemany(sql_模板,数据[“products”])”?@完全不知道你在说什么,因为我是Python初学者。你能告诉我如何改为修复这个()吗?
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()