Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

python - mysqldb .. 'NoneType' object is not subscriptable

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 number of rows. The problem seems to occur here:

player_categories_statistics = cur.fetchone()
player_id = player_categories_statistics[0]

When I try to insert the result, I get:

Traceback (most recent call last):
  File "test2.py", line 72, in <module>
    meat = meatgrind(league_name, categories_measurement_statistics)
  File "test2.py", line 32, in meatgrind
    player_id = int(player_categories_statistics[0])
TypeError: 'NoneType' object is not subscriptable

The code:

import sys 
import MySQLdb
import string 

db = MySQLdb.connect()
cur = db.cursor('localhost','me',XXXXX,'testdb')


def meatgrind(league_name,categories_measurement_statistics):
# a varied range of different categories can be used 

    # 1. list categories
    categories = []
    categories_string = "player_id"
    categories_string_newtable = "meatgrinded_player_id, player_id"
    for category in categories_measurement_statistics:
        categories_string += ", " + category[0]
        categories_string_newtable += ", " + category[0]


    # 2. get players and statistics
    query = "SELECT %s FROM players" % (categories_string)
    cur.execute("%s" % (query))
    # rowcount = int(cur.rowcount)
    rowcount = 2 #hard-coded for debugging

    # 3. meatgrind one player at a time
    meatgrinded_player_id = 1
    for i in range(rowcount):
        player_categories_statistics = cur.fetchone()
        player_id = player_categories_statistics[0]

        #4. grind a category statistic
        meatgrindings_string = "%d, %d" % (meatgrinded_player_id, player_id)

        index = 1
        for category in categories_measurement_statistics:

            # SOME MATH HERE resulting in player_meatgrindings

            meatgrindings_string += ", %0.4f" % player_meatgrindings



query = """INSERT INTO sometable (%s) VALUES (%s)""" % (categories_string_newtable, meatgrindings_string)
cur.execute("%s" % (query))
db.commit()

meatgrinded_player_id += 1


league_name = 'test'
categories_measurement_statistics = (('H', 156.3, 19.643093216474604), ('HR', 21.3, 9.003147597738618), ('SB', 13.25, 16.041179646286754))

meat = meatgrind(league_name, categories_measurement_statistics)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...