Transform varchar to float

0

I made a code that takes data from the DB to generate a graph with matplotlib , but the data in the DB is as varchar and for data creation it must be in the format% with%. How can I do the conversion?

# -*- coding: utf-8 -*-
import MySQLdb as mdb
import matplotlib.pyplot as plt
print(plt)

con = mdb.connect('localhost', 'root', '123456', 'testdb');
null = None
with con:
    cur = con.cursor()
    d = cur.execute("SELECT Ping FROM Pings WHERE Enderecos = 'www.peplink.com'")
    d = cur.fetchall()

    plt.plot(d)
    plt.title("Peplink")
    plt.show() 
    
asked by anonymous 29.05.2017 / 22:31

1 answer

1

I do not know how the MySQLdb API works, but I believe that if you do the cast in the query it should work, assuming Ping is the column that has the varchar value you cited in numeric format :

SELECT CAST(Ping AS DECIMAL(10,6)) as Ping FROM Pings WHERE Enderecos = 'www.peplink.com'

Using DECIMAL(10,6) , but can fine-tune accuracy, I'm not sure what the effect will be after "passing" by the API.

Now if this does not work you can just get the result of Ping in Python itself and do something like this:

 float("10.5")
    
29.05.2017 / 22:36