mysqldb ..'NoneType'对象不可下标 [英] mysqldb .. 'NoneType' object is not subscriptable

查看:77
本文介绍了mysqldb ..'NoneType'对象不可下标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当注释掉cur.execute()db.commit()行时,此代码可以正常工作;即如果我只打印查询,该程序将运行n行.问题似乎发生在这里:

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

代码:

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)

推荐答案

出现错误的原因是:

player_categories_statistics = cur.fetchone()

这将player_categories_statistics设置为None. None[0]引发异常.

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.

以下是我的罪魁祸首 ,您要插入sometable并从players中选择:

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:

这是一个有效的查询,我在一个sqlite3数据库上运行,该数据库具有一个表,该表的单行与您的语句几乎相同,只是为了表明如果数据确实存在,您的表应该可以工作.

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')

这是在sqlite3数据库上的另一个有效查询,该查询具有另一个表且没有行.

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.

还要确保行计数可以按照您希望的方式工作.例如,它与sqlite3不兼容.请参阅 http://www.python.org/dev/peps/pep-0249/#cursor_objects 并查阅MySQLdb文档.

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.

这篇关于mysqldb ..'NoneType'对象不可下标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆