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
return 是一个序列(在本例中是一个元组)或 None
,不是字典。
如果您希望它成为 return 字典,您可以像文档中的这个例子一样替换 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"])
您好,我在执行此命令时收到此错误: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
return 是一个序列(在本例中是一个元组)或 None
,不是字典。
如果您希望它成为 return 字典,您可以像文档中的这个例子一样替换 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"])