tags:

views:

54

answers:

3

I have a very large table (374870 rows) and when I run the following code timestamps just ends up being a long int with the value 374870.... I want to be able to grab all the timestamps in the table... but all I get is a long int :S

import MySQLdb

db = MySQLdb.connect(
                host    = "Some Host",
                user    = "SOME USER",
                passwd  = "SOME PASS",
                db      = "SOME DB",
                port    = 3306

        )

sql = "SELECT `timestamp` from `table`"

timestamps = db.cursor().execute(sql)
+2  A: 

Try this:

cur = db.cursor()
cur.execute(sql)
timestamps = []
for rec in cur:
    timestamps.append(rec[0])
eumiro
This solutions works, Thanks eumiro. still a bit annoyed that fetchall() doesn't work.
Richard
you can change my fourth line into `for rec in cur.fetchall():` and it will work too
eumiro
arg, Brain gone to mush. that is what I was screwing up. fetch functions belong to cursor object :S
Richard
+1  A: 

You need to call fetchmany() on the cursor to fetch more than one row, or call fetchone() in a loop until it returns None.

unwind
the problem is that timestamps ends up being a long int... Maybe due to the large size of my table. so fetchone() fetchmany() or fetchall() will not work for timestamps because a long int object does not have those functions...
Richard
@Richard, 374870 is not really a large table. Try my answer and you will see that the list `timestamps` can accomodate your data. If you tell me what it now looks like, we can try to convert it to any format you need.
eumiro
@Richard: fetchone and friends are **cursor** methods. See my answer.
John Machin
A: 

Consider the possibility that the not-very-long integer that you are getting is the number of rows in your query result.

Consider reading the docs (PEP 249) ... (1) return value from cursor.execute() is not defined; what you are seeing is particular to your database and for portability sake should not be relied on. (2) you need to do results = cursor.fetch{one|many|all}() or iterate over the cursor ... for row in cursor: do_something(row)

John Machin