Python 名称错误:名称';productId';未在pymysql中定义
我想知道如何解决这个问题。我需要通过Pymysql将数据保存到数据库此脚本给了我错误: cursor.execute(sql_模板,{“productId”:productId,“productTitle”:productTitle,“salePrice”:salePrice, NameError:未定义名称“productId”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初学者。你能告诉我如
你如何创建这个“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()