I'm still trying to get the hang of python, so bear with me. please. I have this bit of code that I'm using from a book. The book does not properly show the white space in the code, so the spacing is my best guess. This code is supposed to break the results of a MySQL query into a more readable format.
if form is True:
columns_query = """DESCRIBE %s""" % (table)
print columns_query
columns_command = cursor.execute(columns_query)
headers = cursor.fetchall()
column_list = []
for record in headers:
column_list.append(record[0])
output=""
for record in results:
output = output + "===================================\n\n"
for field_no in xrange(0, len(column_list)):
output = output + column_list[field_no] + ": " + str(record[field_no]) + "\n"
output = output + "\n"
When I try to run it, I get the following:
Traceback (most recent call last):
File "odata_search.py", line 46, in <module>
output = output + column_list[field_no] + ": " + str(record[field_no]) + "\n"
IndexError: tuple index out of range
It has something to do with the str(record[field_no]) portion of the code, but that's what it looks like in the book, so I'm not sure what else to try.
record's length is less thancolumn_list's length. What's inrecord?recordis a loop variable from a previous line. Did you mean to useheadersorcolumn_listinstead?