发布从亚马逊的API上保存python上的MySql数据

时间:2017-08-24 08:46:19

标签: python mysql-python

我正在使用以下内容传递有效负载字典。 有效负载打印

('B01MTOV8IP', '35006', '23.95', '', 'now', 'Usually ships in 24 hours', 'https://www.amazon.com/reviews/iframe?akid=AKIAIDCPAFSAQICDTFNQ&alinkCode=xm2&asin=B01MTOV8IP&atag=reakenture-20&exp=2017-08-25T08%3A36%3A25Z&v=2&sig=QtRIZIBfjqq%252BPuYaUD%252BsoIxXSy2dRzYqCACDFm%252B%252BtV8%253D', 'CHG-GSTWL')

然后我收到此错误:

我试图更改值并设定期限,我添加了str(值),我也尝试将其放在字典中。我只需要将这些sku及其数据发送到数据库。

当我改回%s时,我仍然会收到此错误 然后我收到了这个错误:

文件“/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py”,第36行,在defaulterrorhandler中     提出错误类,错误值 _mysql_exceptions.OperationalError:(1582,“对本机函数'IsNull'的调用中的参数计数不正确”) [完成时间为3.2秒,退出代码为1]

I also tried 
    payload =(
    asin,
    bsr,
    str(selling_price_v),
    str(listing_price_v),
    availability_type,
    availability,
    reviews,
    sku)

我的目标是将非空值保存到mysql数据库中。

以下是数据类型的模型

class BSR(models.Model):
    ItemSKU = models.CharField(primary_key=True,max_length=200, blank=True)
    list_price = models.DecimalField(max_digits=6, blank=True, decimal_places=2)
    selling_price = models.DecimalField(max_digits=6, blank=True, decimal_places=2)
    availability = models.CharField(max_length=200, blank=True)
    Best_Sellers_Rank = models.IntegerField(max_length=200, blank=True)
    AISN = models.CharField(max_length=200, blank=True)

    class Meta:
        ordering = ['ItemSKU']
        verbose_name_plural = "BSR TEMP DATA"

    def __str__(self):
        return self.ItemSKU

CODE

for sku in set(SKUS):
    payload = []
    try:
        time.sleep(2)
        product = amazon.lookup(ItemId=sku, IdType="SKU",SearchIndex='All')
        bsr = product.sales_rank
        print bsr
    except Exception as e:
        bsr=''
        print bsr
    # try:
    #     fprice = product.formatted_price#.strip("$").strip(".")
    #     print fprice
    # except Exception as e:
    #     fprice = "n/a"
    #     print fprice
    #     print e
    try:
        asin = product.asin#.strip(".")
        print asin
    except Exception as e:
        asin = ""
        print asin

    try:
        listing_price = product.listing_price
        # print selling_price[0]#type
        print listing_price[0]#price
        listing_price_v = listing_price[0]
    except Exception as e:
        listing_price_v = ""
        print listing_price_v
    # try:
    #     price = product.price
    #     print price#type
    #     # print selling_price[1]#price
    # except Exception as e:
    #     price = "n/a"
    #     print price
    #     print e
    try:
        reviews = product.reviews[1]
        print reviews
    except Exception as e:
        reviews = ""
        print reviews

    try:
        availability_type = product.availability_type
        print availability_type
    except Exception as e:
        availability_type = ""
        print availability_type
    try:
        availability = product.availability
        # if 
        print availability
    except Exception as e:
        availability = ""
        print availability

    try:
        selling_price = product.price_and_currency
        selling_price_v = selling_price[0]#type
        print selling_price_v
    except Exception as e:
        selling_price = ""

    conn = MySQLdb.connect(host="serveraddress", user="un", passwd="pw", db="API")
    payload =[
    asin,
    bsr,
    str(selling_price_v).replace('.',''),
    str(listing_price_v).replace('.',''),
    availability_type,
    availability,
    reviews,
    sku]
    print payload 
    # conn = sqlite3.connect('skubsr.db')
    c = conn.cursor(as_dict=True)
    c.execute("""UPDATE webservice_bsr 
    SET 
    AISN = IsNull(@AISN, %s),
    Best_Sellers_Rank = IsNull(@Best_Sellers_Rank, %s)
    selling_price = IsNull(@selling_price, %s),
    listing_price = IsNull(@listing_price, %s),
    availability_type = IsNull(@availability_type, %s),
    availability = IsNull(@availability, %s),
    reviews = IsNull(@reviews, %s)
    WHERE ItemSKU = %s""", payload)
    conn.commit()

1 个答案:

答案 0 :(得分:0)

我猜你误解了函数isnull

你可以在这里阅读:https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_isnull 该函数只接受一个参数,并返回参数为NULL或不返回。 您可能想要使用的是https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_coalesce

isnull替换为COALESCE,然后尝试一下。