This code works fine when the cur.execute()
and db.commit()
lines are commented out; i.e. if all I do is print the query, this program runs for n n
The reason for your error is:
player_categories_statistics = cur.fetchone()
This sets player_categories_statistics
to None
. None[0]
raises the exception.
The only reason this would happen is your query returns no rows, which means your table is empty. Your table is most likely is empty because you never put any rows in it, or less likely you removed them somehow.
I culprit may be the following, you are inserting into sometable
and selecting from players
:
INSERT INTO sometable (%s) VALUES (%s)
vs
SELECT %s FROM players
The only reason this is possible is because your forcing it to loop even if nothing was returned with the line:
rowcount = 2 #hard-coded for debugging
Additional Info:
Here's a working query I ran on an sqlite3 database with a single table with a single row with nearly identical statements as yours, just to show that yours should be working if the data is indeed there.
query = "SELECT %s FROM customer" % 'first_name, last_name'
row = c.execute("%s" % (query)).fetchone()
row
Out[28]: (u'Derek', u'Litz')
Here's another working query on a sqlite3 database with another table and no rows.
query = "SELECT %s FROM customer2" % 'first_name, last_name'
print c.execute("%s" % (query)).fetchone()
None
As you can see, identical to the behavior above.
Also make sure rowcount works they way you want with your DB. It doesn't with sqlite3, for example. See rowcount spec in http://www.python.org/dev/peps/pep-0249/#cursor_objects and consulte MySQLdb docs.