Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python---TypeError:';非类型';对象没有属性'__获取项目';_Python - Fatal编程技术网

Python---TypeError:';非类型';对象没有属性'__获取项目';

Python---TypeError:';非类型';对象没有属性'__获取项目';,python,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, pa

我试图运行以下代码:

这是我为了提高自己而做的事情

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

当我尝试运行此命令时,会出现以下错误:


回溯(最近一次呼叫最后一次):
文件“main.py”,第29行,在
增加课程(“用户”,“第1课”)
文件“main.py”,第24行,在add_课程中
cursor.fetchone()[2][lesson]=0
TypeError:“非类型”对象没有属性“\uuuu getitem\uuuu”


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

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

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

您的
游标。fetchone()
返回了
,请在查询结果之前检查它是否正常工作。谢谢你的帮助:)
cursor.execute("""select * from maintable where kullad="?"""", (username,))