pymysql 只是 select 值并保存到变量中

pymysql just select value and save into variable

cursor.execute("SELECT price FROM tabel WHERE id = 1600")
print cursor.fetchone()

给我输出

{u'price': u'4345.6'}

如何 select 仅将值 4345.6 保存在变量中以进行一些数学运算?

您可以通过以下代码轻松解决此问题。

data = cursor.fetchone() #get the data in data variable
value = float(data['price']) # load the data into value with conversion of its type

您可以使用Dictionary.Get方法:

https://www.w3schools.com/python/ref_dictionary_get.asp

cursor.execute("SELECT price FROM tabel WHERE id = 1600") 
resultdict = cursos.fetchone() 
result = resultdict.get("price") 
print(result)