我这是一个获取实时汇率的方法,刚开始写法如下就是一直会报一个数据类型错误
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"