Transform long/varchar data to string

Asked

Viewed 52 times

0

# -*- 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 CAST(Ping AS DECIMAL(10,6)) as Ping FROM Pings WHERE Enderecos = 'www.peplink.com'")
    d = cur.fetchall()
    e = cur.execute("SELECT Timestamp FROM Pings WHERE Enderecos = 'www.peplink.com'")
    a = len(e)
    print a
    e = cur.fetchall()
    #plt.plot(d, e)
    #plt.title("Peplink")
    #plt.show() 

He makes the following mistake:

    a = len(e)
    TypeError: object of type 'long' has no len()
  • 2

    And if you do a = len(str(e))?

2 answers

2


If that was your problem, you’d just do len(str(e)) - but it wouldn’t solve anything - you would just print out the number number number number "and" (that is, if 400 records had been read, this would result in "3") The number returned by the cursor.execute is already the number of affected lines - in some database drivers. In others, you need to look at the property .rowcount cursor.

To actually get the results of the query, after the call to cur.execute(...) calling cur.fetchall() - this will return all results in the form of a Python list.

0

If the goal is to get the number of rows returned in the query, you can use the "rowcount", which is a property of the cursor referring to the last "run":

e = cur.execute("SELECT Timestamp FROM Pings WHERE Enderecos = 'www.peplink.com'")
a = cur.rowcount   # a = len(e)
print a

Browser other questions tagged

You are not signed in. Login or sign up in order to post.