【问题标题】:I am getting a NoneType error when trying to work with Python and Sqlite3尝试使用 Python 和 Sqlite3 时出现 NoneType 错误
【发布时间】:2020-04-05 19:13:51
【问题描述】:
cursor.execute(f"SELECT user_id, Class, Health, Mana, Defense, Maxmana, Maxhealth, Attack, Critical FROM savefile WHERE user_id = '{message.author.id}'")
           result1 = cursor.fetchone()
           classs = int(result1[1]); health = int(result1[2]); mana = int(result1[3]); defense = int(result1[4]); attack = int(result1[7]); critical = int(result1[8])
           sql = "UPDATE savefile SET Class = ? AND SET Health = ? AND SET Mana = ? AND SET Defense = ? AND SET Maxmana = ? AND SET Maxhealth = ? AND SET Attack = ? AND SET Critical = ? WHERE user_id = ?"
           val = (classs + classplus, str(message.author.id),
                  health + healthplus, str(message.author.id),
                  mana + manaplus, str(message.author.id),
                  defense + defenseplus, str(message.author.id),
                  attack + attackplus, str(message.author.id),
                  critical + criticalplus, str(message.author.id))
           cursor.execute(sql, val)
           db.commit()

这是我正在使用的代码块。我目前正在制作一个不和谐的机器人,并且有几个其他命令可以添加到一列中。但是,在“classs = ...”行,即上面显示的代码中的第三行,(额外的 S 是故意的)我收到此错误:

TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

我一般对使用 sqlite 比较陌生,不明白为什么会发生这种情况。我的其他机器人命令中有类似的代码,但不明白为什么这特别不起作用。提前感谢您的帮助!

【问题讨论】:

    标签: python sql database sqlite python-sql


    【解决方案1】:

    这些结果之一(result1[i])很可能是 NoneType,这意味着没有从数据库中检索到数据:

    classs = int(result1[1]); health = int(result1[2]); mana = int(result1[3]); defense = int(result1[4]); attack = int(result1[7]); critical = int(result1[8])
    

    【讨论】:

    • 对不起,如果这是一个新手问题,但你知道我该如何解决这个问题吗?
    • 尝试:for i in result1: print(type(i))。您将看到数据库中哪些结果具有 None 值。之后,您可能需要在将其转换为 int 并分配默认值之前检查该值是否为 None。或者你有可能搞砸了索引:我看到你从 result1[1] 开始,也许你想要 result1[0] 作为那个值?
    猜你喜欢
    • 1970-01-01
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    • 2012-08-19
    • 1970-01-01
    • 2019-08-10
    • 1970-01-01
    相关资源
    最近更新 更多