James
James

Reputation: 168

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

Hi I'm getting this error: TypeError: tuple indices must be integers or slices, not str in command on this command and I'm a little unsure where I'm going wrong here. Here is the code I'm working with:

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

Upvotes: 1

Views: 247

Answers (1)

Patrick Haugh
Patrick Haugh

Reputation: 61014

fetchone returns a sequence (in this case a tuple) or None, not a dictionary.

If you would like it to return a dictionary, you can replace Connection.row_factory like this example from the documentation:

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

Upvotes: 2

Related Questions