Question

Here's a shortened version of my script:

import MySQLdb
src_db = MySQLdb.connect(**some_connection)
src_cursor = src_db.cursor()
v = src_cursor.execute('SELECT node_id FROM stats WHERE time_unit >= 1388534400')

v ends up being of long type, which I cannot understand. I expect to have a generator that would return 1-element tuples (I ask only for one column). And it returns a long value, being number of rows returned from db. Why?

When I try to iterate through it:

node_ids = {int(x[0]) for x in v}

I get following error:

TypeError: 'long' object is not iterable
Was it helpful?

Solution

You need to read the python database API specification, PEP-249.

Basically, after you've executed a query with a cursor object, you then query the cursor object for the results.

cursor.execute(my_sql)

for record in cursor.fetchall():
    # do stuff

OTHER TIPS

src_cursor.execute(SELECT_QUERY) will return the number of rows matching your query.

To iterate through the result of the query:

for row in src_cursor.fetchall():

To get one row at a time:

row = src_cursor.fetchone()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top