【问题标题】:Why record = cur.fetchone()[0] is all of a sudden a Nonetype为什么 record = cur.fetchone()[0] 突然变成了 Nonetype
【发布时间】:2020-06-16 13:29:34
【问题描述】:

所以我正在研究一个不和谐的货币系统。我用下面的代码给别人钱。

@client.command()
async def give_money(ctx, member: discord.Member, amount: int):
  connection = sqlite3.connect("testdatabase.db")
  cur = connection.cursor()
  cur.execute(f"UPDATE currency_value_table SET currency = currency - {amount} WHERE   
member_id = {ctx.author.id}")
  record = cur.fetchone()[0]
  if record < amount:
    return await ctx.send("Bruh you too poor to do that!")
  else:
    await ctx.send(f"{amount} given to {member.display_name}")
  cursor.execute(f"UPDATE currency_value_table SET currency = currency + {amount} WHERE member_id = {member.id}")
  connection.commit()
  connection.close()
  database_record = cur.fetchone()[0]
  money = database_record
  await ctx.send(f"You are clearly a broke college student.. you have {money} dollars")

但是,当我运行它时,我会收到以下消息:

File "discord.bot.py", line 398, in give_money
record = cur.fetchone()[0]
TypeError: 'NoneType' object is not subscriptable

我不确定这是一个无类型,因为如果我运行它:

@client.command()
async def bal(ctx):
  connection = sqlite3.connect("testdatabase.db")
  cur = connection.cursor()
  cur.execute(f"SELECT currency FROM currency_value_table WHERE member_id = {ctx.author.id}")
  record = cur.fetchone()[0]
  connection.commit()
  connection.close()
  await ctx.send(f"You are clearly a broke college student.. you have {record} dollars")
  cur.execute(f"SELECT currency FROM currency_value_table WHERE member_id = {ctx.author.id}")

工作正常吗?

【问题讨论】:

    标签: python database sqlite discord discord.py


    【解决方案1】:

    这是因为您执行了 UPDATE 语句而不是 SELECT 语句,后者不返回任何数据。在这种情况下,最好先执行SELECT 语句以检查当前货币金额,然后再更新表。

    cur.execute(f"SELECT currency FROM currency_value_table WHERE member_id = {ctx.author.id}")
    record = cur.fetchone()[0]
    

    【讨论】:

      【解决方案2】:

      请删除connection.close(),它不是必需的,检查一次!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-27
        • 2019-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多