下面是我的代码。Function Combiner()给出不正确的值:
[(Decimal('3213.713766'),)] [(Decimal('7.041164'),)]
我只需要值3213.713766 7.041164
def data_generation():
con=mysql.connector.connect()
cursor=con.cursor()
cursor.execute("CALL `Pulse`.`SP_GetAverageBillRate`();")
data=cursor.fetchall()
return data
def data_generation2():
con=mysql.connector.connect()
cursor=con.cursor()
cursor.execute("CALL `Pulse`.`SP_GetAverageGrossProfit`();")
data2=cursor.fetchall()
return data2
def combiner():
AverageBilling = data_generation()
AverageGrossProfit = data_generation2()
return [AverageBilling, AverageGrossProfit]
如何从此返回的对象中获取值?
答案 0 :(得分:0)
足够简单的修补程序,fetchone
而不是全部,因此您不会获得结果列表。您将获得一个包含一个元素的元组,可以使用[0]
选择该元素,然后将结果强制转换为float
:
def data_generation():
con=mysql.connector.connect("connection_string")
cursor=con.cursor()
cursor.execute("CALL `Pulse`.`SP_GetAverageBillRate`();")
data = float(cursor.fetchone()[0])
return data
def data_generation2():
con=mysql.connector.connect("connection_string")
cursor=con.cursor()
cursor.execute("CALL `Pulse`.`SP_GetAverageGrossProfit`();")
data2 = float(cursor.fetchone()[0])
return data2
def combiner():
AverageBilling = data_generation()
AverageGrossProfit = data_generation2()
return [AverageBilling, AverageGrossProfit]
答案 1 :(得分:0)
DBAPI之后的所有python数据库连接器都将为行返回元组,即使您仅选择了一列(或像您一样,也称为返回单个值的函数)。
这意味着每次调用if let textValue = username.text {
if textValue.contains("\'") || textValue.contains("\"") {
print("Show Alert")
} else {
print("Passed")
}
} else {
print("username.text is nil do something or just ignore")
}
时,总是会得到一个元组,可能只包含一个项目。
您可以通过从元组中检索索引0处的项来获得该单一值:
fetchone()
或一行:
row = cursor.fetchone()
data = row[0]
data2 = float(data)