sqlite3.OperationalError: no such column: | Discord.py

sqlite3.OperationalError: no such column: | Discord.py

当我在尝试更改当前前缀时遇到此错误时尝试执行更改前缀命令时:

sqlite3.OperationalError: no such column: e

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\achut\AppData\Roaming\Python\Python39\site-packages\discord\ext\commands\bot.py", line 903, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\achut\AppData\Roaming\Python\Python39\site-packages\discord\ext\commands\core.py", line 859, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\achut\AppData\Roaming\Python\Python39\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: OperationalError: no such column: e

命令的代码是这样的:

@client.command()
@commands.has_permissions(manage_guild=True)
async def change_prefix(ctx, newprefix: str):
    if len(newprefix) > 5:
        await ctx.send("The server prefix cannot be less than 5 characters in length!")

    else:
        cursor.execute(f"UPDATE guilds SET prefix = {newprefix} WHERE serverid = {ctx.guild.id}")
        connection.commit()
        await ctx.send(embed = discord.Embed(title = f"Prefix Changed to `{newprefix}`",description = f"The bot's server prefix was just changed to `{newprefix}`!", colour=0x04ff00))

抱歉,如果这可能是一个简单的修复,但我真的不擅长 sqlite3。谢谢!

您只需要在 SQL 命令中用单引号将 {newprefix} 括起来。有了这个,用户将不需要在 Discord 上添加引号。

cursor.execute(f"UPDATE guilds SET prefix = '{newprefix}' WHERE serverid = {ctx.guild.id}")