'pymysql just select value and save into variable

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

gives me the output

{u'price': u'4345.6'}

how can I select just the value 4345.6 and save it in a variable to do some math?



Solution 1:[1]

You can easily solve this by the following peace of code.

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

Solution 2:[2]

You can use Dictionary.Get Method:

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Muhammad Ilyas
Solution 2