【问题标题】:Issue saving MySql data on python from Amazon's API从 Amazon 的 API 在 python 上保存 MySql 数据的问题
【发布时间】:2017-08-24 08:46:19
【问题描述】:

我正在传递带有以下内容的有效负载字典。 有效载荷打印

('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(value),还尝试将其放入字典中。我只需要将这些 sku 及其数据发送到数据库即可。

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

文件“/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py”,第 36 行,在默认错误处理程序中 引发错误类,错误值 _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

代码

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()

【问题讨论】:

    标签: python mysql-python


    【解决方案1】:

    我猜你误解了函数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 并试一试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-20
      • 2011-10-29
      • 2011-08-30
      • 2012-04-19
      • 2011-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多