0

I am querying all the fields of a mysql table like this -

query = """select * from %s where %s=%s;""" % (tableName,key,value)
cursor.execute(query)

Now, I would like to iterate over the results. I do not want to specify the column names which I would like to fetch as I would like to fetch all. This is part of a generic migration verification script which is supposed to do the verification for all the mysql tables. Since there will be different number of columns in different tables, I would like a general solution.

I checked https://stackoverflow.com/a/25347195/351903 but it involves specific fields.

2 Answers 2

2

MySQLdb conforms to PEP-249.

Therefore,execute must return an iterator. You can just do:

for tupl in cursor.execute(query):
    pass

where tupl is a tuple.


*Alternatively, you can use fetchone, fetchmany(n) or fetchall() to evaluate at once.

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

1 Comment

Yes it works. I did some wrong observation it seems.
0

First run below query

sp_help Table

then get the column names from that

after that run your code. Because you will have column names.

I also created this type of utility. That was windows console app.

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.