【问题标题】:discord.py SQLite getting row contentdiscord.py SQLite 获取行内容
【发布时间】:2021-02-20 05:05:57
【问题描述】:
@client.command()
async def tag(ctx, tag):
    sql.execute(f'select tags_name from tags_list')
    does_exist = sql.fetchone()
    print(does_exist)

    if does_exist is not None:
        sql.execute(f'SELECT tags_content FROM tags_list')
        final = sql.fetchall()
        await ctx.send(final[0])
    else:
        await ctx.send(f"Tag named `{tag}` doesnt exists!")

所以您在上面看到的代码用于从表tags_list 中获取内容。

上面的图片是桌子tags_list。当我调用命令时,我正在尝试获取tags_content。但是例如当我调用像.tag test 这样的命令时,我希望它给我test,因为它们在同一行。但相反,它从第一行给出tags_content。所以它给出了h 而不是test。如何指定要从中获取内容的行?

编辑:这是我运行命令.tag test时得到的:('h',)

【问题讨论】:

  • 你有一个名为tag的方法,它有一个名为tag的参数(不是一个好主意..)代码对tag参数有什么作用?
  • @balderman tag 等于数据库中的tags_name,它应该发送tags_content 的内容。正如我在问题中所说的那样。
  • 据我了解,tag 参数不是 sql 查询的一部分。对吗?
  • @balderman 是的,它在数据库中不存在。它仅适用于命令
  • 我认为你需要有 1 个 sql 查询:select tags_name,tags_content from tags_list where tag_name = <your tag> - 不是吗?

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


【解决方案1】:

当您从 SQLite 表中选择一行时,您可以使用 WHERE 指定您想要的行。例如:

SELECT tags_content FROM tags_list WHERE tags_name='test'

因此您可以在选择行时使用tag 参数指定tags_name

@client.command()
async def tag(ctx, tag):
    sql.execute(f'SELECT tags_content FROM tags_list where tags_name = "{tag}"')
    final = sql.fetchone()
    if final:
        await ctx.send(final)
    else:
        await ctx.send(f"Tag named `{tag}` doesnt exists!")

【讨论】:

  • 谢谢,这行得通。但是当我将其更改为 SELECT tags_content FROM tags_list where tags_name = "{tag}" 时它起作用了。
猜你喜欢
  • 2021-01-28
  • 1970-01-01
  • 2021-06-17
  • 2021-02-18
  • 1970-01-01
  • 2022-08-15
  • 2016-10-30
  • 2021-07-10
  • 2022-09-23
相关资源
最近更新 更多