0

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)

3 Answers 3

2

Try this:

cur = db.cursor()
cur.execute(sql)
timestamps = []
for rec in cur:
    timestamps.append(rec[0])
Sign up to request clarification or add additional context in comments.

3 Comments

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

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

3 Comments

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, 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.
@Richard: fetchone and friends are cursor methods. See my answer.
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.