2019-01-30
阅读量:
947
如何将api调用保存到mysql数据库中?
尝试将API调用保存到mysql数据库中提示以下报错:
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()
错误:
/ Users / reezalaq / PycharmProjects / Aliexpress / venv / bin / python /Users/reezalaq/Downloads/newali/script.py
Traceback(最近一次调用最后一次):
文件“/Users/reezalaq/Downloads/newali/script.py “,第28行,
打印('%s%s%s%s'%(产品['productId'],产品['productTitle'],产品['salePrice'],产品['originalPrice']))
TypeError :字符串索引必须是整数
进程以退出代码1结束






评论(0)


暂无数据
推荐帖子
0条评论
0条评论
0条评论