【问题标题】:TypeError: tuple indices must be integers or slices, not str in commandTypeError:元组索引必须是整数或切片,而不是命令中的 str
【发布时间】:2019-04-09 21:28:15
【问题描述】:

嗨,我收到此错误:TypeError: tuple indices must be integers or slices, not str in command 在此命令上,我有点不确定我在哪里出错了。这是我正在使用的代码:

@checks.can_embed()
@commands.command(name="botinfo")
async def botinfo(self, ctx: UKGCtx):
    """Shows advanced information about the bot."""
    char_count = 0
    deaths_count = 0
    levels_count = 0
    with closing(userDatabase.cursor()) as c:
        c.execute("SELECT COUNT(*) as count FROM chars")
        result = c.fetchone()
        if result is not None:
            char_count = result["count"]
        c.execute("SELECT COUNT(*) as count FROM char_deaths")
        result = c.fetchone()
        if result is not None:
            deaths_count = result["count"]
        c.execute("SELECT COUNT(*) as count FROM char_levelups")
        result = c.fetchone()
        if result is not None:
            levels_count = result["count"]

【问题讨论】:

    标签: python python-3.x discord.py discord.py-rewrite


    【解决方案1】:

    fetchone 返回一个序列(在本例中为元组)或None,而不是字典。

    如果你希望它返回一个字典,你可以替换 Connection.row_factory 像文档中的这个例子:

    import sqlite3
    
    def dict_factory(cursor, row):
        d = {}
        for idx, col in enumerate(cursor.description):
            d[col[0]] = row[idx]
        return d
    
    con = sqlite3.connect(":memory:")
    con.row_factory = dict_factory
    cur = con.cursor()
    cur.execute("select 1 as a")
    print(cur.fetchone()["a"])
    

    【讨论】:

      猜你喜欢
      • 2017-03-11
      • 2019-11-23
      • 2018-12-29
      • 2021-01-18
      • 2018-03-12
      • 1970-01-01
      • 2015-12-09
      • 2019-02-05
      • 2021-11-28
      相关资源
      最近更新 更多