0

I have a very large table in mysql that I'm accessing through a python script, and I'd like the output of my select query to be stored as a list. Here's what I have:

import MySQLdb
db = MySQLdb.connect(host='', user='', passwd='', db='')
cursor = db.cursor()

sqlselect = "SELECT desig FROM table WHERE num=0;"
desigs = cursor.execute(sqlselect)
print desigs

But this just gives me the number of rows in set which is nearly 250,000. Instead, I'd like it to print a list of each 'desig'. How can I do this?

3
  • print [desig for desig in cursor] Commented Sep 20, 2017 at 22:44
  • @OluwafemiSule you can't iterate on an integer ;) Commented Sep 20, 2017 at 22:45
  • That should be cursor and not desigs right after execute is called. Commented Sep 20, 2017 at 22:47

1 Answer 1

1

cursor.execute() will return the number of rows modified or retrieved.

Once you've executed the query, run cursor.fetchall(), which will return you an array containing each row in your query results. That is, you get an array of arrays.

Edit: list! list of lists!! argh

Sign up to request clarification or add additional context in comments.

2 Comments

Don't you mean list of lists?
@Barmar dang it, I had just used array to describe a list somewhere else, and it leaked into stackexchange, how embarrassing. I'll leave it up to teach myself a lesson.

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.