簡體   English   中英

TypeError:元組索引必須是整數或切片,而不是命令中的str

[英]TypeError: tuple indices must be integers or slices, not str in command

嗨,我收到此錯誤: TypeError: tuple indices must be integers or slices, not str in command此命令中的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"]

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM