mysqldb .. 'NoneType' object is not subscriptable

后端 未结 1 1784
误落风尘
误落风尘 2020-12-11 12:13

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

相关标签:
1条回答
  • 2020-12-11 12:56

    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.

    0 讨论(0)
提交回复
热议问题