【问题标题】:sqlite3.OperationalError: no such column: | Discord.pysqlite3.OperationalError:没有这样的列:|不和谐.py
【发布时间】:2021-04-08 03:41:36
【问题描述】:

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

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 上真的很糟糕。谢谢!

【问题讨论】:

  • 假设 prefix 是数据库中的一个 VARCHAR 列,您设置的值必须用单引号括起来。
  • 我在 discord 上用单引号试过了,效果很好,但是有什么方法可以让用户输入不带单引号的前缀?
  • 如果您在 SQL 语句中使用占位符值,那么它会自动为您添加引号。 cursor.execute("UPDATE guilds SET prefix = %s WHERE serverid = %s", [newprefix, ctx.guild.id])
  • 你能告诉我一个如何在 SQL 语句中放置占位符值的例子吗?
  • 我得到这个错误:sqlite3.OperationalError: near "%": syntax error

标签: python sqlite discord discord.py discord.py-rewrite


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    • 2021-01-12
    • 2021-01-27
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多