mysqldb .. 'NoneType' object is not subscriptable

杀马特。学长 韩版系。学妹 提交于 2019-11-28 13:04:41

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!