Python --- TypeError:' NoneType'对象没有属性' __ getitem __'

时间:2015-12-16 07:46:12

标签: python

我试图运行此代码:

这就是我正在做的事情,以改善自己...

import sqlite3 as sq


def add_user(username, passwd):
    database = sq.connect("database.db")
    cursor = database.cursor()
    cursor.execute("""insert into maintable values (?, ?, ?)""",
                   (username, passwd, ""))
    database.commit()


def add_lesson(username, lesson):
    database = sq.connect("database.db")
    cursor = database.cursor()
    cursor.execute("""select * from maintable where kullad=?""", (username,))
    if cursor.fetchone()[2] != "":
        lessons_dict = {lesson: 0}
        cursor.execute(
            """update maintable set lessons=? where kullad=?""", (lessons_dict, username,))
        database.commit()

    else:
        cursor.fetchone()[2][lesson] = 0
        cursor.execute(
            """update maintable set lessons=? where kullad=?""", (lessons_dict, username,))
        database.commit()

add_lesson("user", "lesson1")

当我尝试运行时,我收到此错误:

Traceback (most recent call last):
  File "main.py", line 29, in <module>
    add_lesson("user", "lesson1")
  File "main.py", line 24, in add_lesson
    cursor.fetchone()[2][lesson] = 0
TypeError: 'NoneType' object has no attribute '__getitem__'

1 个答案:

答案 0 :(得分:0)

我认为您的SQL可能是这样的:

cursor.execute("""select * from maintable where kullad="?"""", (username,))
SQL中的

字符串应该用引号括起来。