繁体   English   中英

Python — TypeError:'NoneType'对象没有属性'__getitem__'

[英]Python — TypeError: 'NoneType' object has no attribute '__getitem__'

我正在尝试运行以下代码:

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

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

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

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

SQL中的字符串应用引号引起来。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM