1. ホーム
  2. python

Python2 error connecting to mysql to get value TypeError: unsupported operand type(s) for /: 'float' および 'NoneType'

2022-02-19 10:34:25
<パス

私はライブの為替レートを取得するメソッドを持っており、最初は次のように書きましたが、データ型エラーが報告され続けました。

為替レートを取得する

def get_rate(self, time):

    # Where USD is the base currency you want to use
    url = 'https://v3.exchangerate-api.com/bulk/fa192ccf2d553fe2b4005125/USD'
    # Making our request
    try:
        sql = "SELECT rate, savetime from JD_rate"
        self.cur_test.execute(sql)
        rate = self.cur_test.fetchone()[0]
        savetime = self.cur_test.fetchone()[1].encode("utf-8")[:11]
        if savetime ! = time:
            response = requests.get(url)
            rate = response.json()['rates']['IDR']
            sql1 = "UPDATE JD_rate set rate='%s', savetime='%s' WHERE id = 1" % (rate, time)
            self.cur_test.execute(sql1)
            self.coon_test.commit()
        return rate
    except Exception, ex:
        traceback.print_exc()
        print ex, "get_rate"


訂正:(主に、カーソルが値を取得した後、直接タプルフェッチを行うことができないため、最初に保存定義を行う必要があります。)

為替レートを取得する

def get_rate(self, time):

    # Where USD is the base currency you want to use
    url = 'https://v3.exchangerate-api.com/bulk/fa192ccf2d553fe2b4005125/USD'
    # Making our request
    try:
        sql = "SELECT rate, savetime from JD_rate"
        self.cur_test.execute(sql)
        result_a = self.cur_test.fetchone()[0]
        rate = result_a[0]

        savetime = result_a[1].encode("utf-8")[:11]
        if savetime ! = time:
            response = requests.get(url)
            rate = response.json()['rates']['IDR']
            sql1 = "UPDATE JD_rate set rate='%s', savetime='%s' WHERE id = 1" % (rate, time)
            self.cur_test.execute(sql1)
            self.coon_test.commit()
        return rate
    except Exception, ex:
        traceback.print_exc()
        print ex, "get_rate"