我如何检查是否已经存在相同的标签名称?
How do i make it to check if there is a same tags name already exist?
我创建了一个自定义命令,更像是自定义标签命令,但我想让它检查一下,如果有相同的标签名称,请发送一条消息,说明具有此名称的标签已经存在
我尝试使用这段代码,但它一直将新标签记录到 sqlite 中,尽管它已经存在。
@tags.command()
async def create(self, ctx, tag=None, *,text=None):
db = sqlite3.connect('main.db')
cursor = db.cursor()
cursor.execute(f"SELECT text FROM tags WHERE guild_id = {ctx.guild.id} and tag")
result = cursor.fetchone()
if result is None:
sql = (f"INSERT INTO tags(guild_id, tag, text) VALUES(?,?,?)")
val = (ctx.guild.id, tag, text)
await ctx.send(f'Created {tag}!')
cursor.execute(sql, val)
db.commit()
if result is not None:
await ctx.send(f'Tag named {tag} was already exist.')
cursor.close()
db.close()
查询的 WHERE
子句必须包含列 tag
:
的条件
cursor.execute(f"SELECT text FROM tags WHERE guild_id = {ctx.guild.id} and tag = {tag}")
也许不需要检查 guild_id
:
cursor.execute(f"SELECT text FROM tags WHERE tag = {tag}")
或使用 ?
占位符:
cursor.execute("SELECT text FROM tags WHERE tag = ?", (tag,))
我创建了一个自定义命令,更像是自定义标签命令,但我想让它检查一下,如果有相同的标签名称,请发送一条消息,说明具有此名称的标签已经存在 我尝试使用这段代码,但它一直将新标签记录到 sqlite 中,尽管它已经存在。
@tags.command()
async def create(self, ctx, tag=None, *,text=None):
db = sqlite3.connect('main.db')
cursor = db.cursor()
cursor.execute(f"SELECT text FROM tags WHERE guild_id = {ctx.guild.id} and tag")
result = cursor.fetchone()
if result is None:
sql = (f"INSERT INTO tags(guild_id, tag, text) VALUES(?,?,?)")
val = (ctx.guild.id, tag, text)
await ctx.send(f'Created {tag}!')
cursor.execute(sql, val)
db.commit()
if result is not None:
await ctx.send(f'Tag named {tag} was already exist.')
cursor.close()
db.close()
查询的 WHERE
子句必须包含列 tag
:
cursor.execute(f"SELECT text FROM tags WHERE guild_id = {ctx.guild.id} and tag = {tag}")
也许不需要检查 guild_id
:
cursor.execute(f"SELECT text FROM tags WHERE tag = {tag}")
或使用 ?
占位符:
cursor.execute("SELECT text FROM tags WHERE tag = ?", (tag,))