【问题标题】:sqlite3.OperationalError: near “<”: syntax errorsqlite3.OperationalError:靠近“<”:语法错误
【发布时间】:2021-10-19 03:33:30
【问题描述】:

第 115 行,添加 self.cursor.execute(f'UPDATE GuildTable SET GUILD_NAME = {str(guild_name_msg)} WHERE GUILD_IDS = {ctx.guild.id}') sqlite3.OperationalError:靠近“

self.cursor.execute(f'UPDATE GuildTable SET GUILD_NAME = {str(guild_name_msg)} WHERE GUILD_IDS = {ctx.guild.id}')

sqlite3.OperationalError:靠近“

我该怎么办?

        try:                
            guild_name_msg = await self.bot.wait_for('message', check=check, timeout=30)

            self.cursor.execute(f"SELECT GUILD_NAME FROM GuildTable WHERE GUILD_IDS = {ctx.guild.id}")                
            result = self.cursor.fetchone()
            if result is None:
                self.cursor.execute(f'INSERT INTO GuildTable(GUILD_NAME) VALUES({str(guild_name_msg)})')
            elif result is not None:
                self.cursor.execute(f'UPDATE GuildTable SET GUILD_NAME = {str(guild_name_msg)} WHERE GUILD_IDS = {ctx.guild.id}')
                    
            self.db.commit()
            self.cursor.close()
            self.db.close()

【问题讨论】:

  • 尝试使用 dict 或 tuple 代替 f 字符串

标签: python sql python-3.x discord.py


【解决方案1】:
try:
    guild_name_msg = await self.bot.wait_for('message', check=check, timeout=30)

    self.cursor.execute(f"SELECT GUILD_NAME FROM GuildTable WHERE GUILD_IDS = :guild_id", {"guild_id": ctx.guild.id})
    result = self.cursor.fetchone()
    if result is None:
        self.cursor.execute(f'INSERT INTO GuildTable(GUILD_NAME) VALUES (?)',guild_name_msg)
    elif result is not None:
        self.cursor.execute(
            f'UPDATE GuildTable SET GUILD_NAME = :guild_name_msg WHERE GUILD_IDS = :guild_id',{"guild_id":ctx.guild.id,"guild_name_msg":guild_name_msg})

    self.db.commit()
    self.cursor.close()
    self.db.close()

我认为你的错误是它做的第一件事是组合字符串,然后在 SQL 中运行不佳,但是如果你在 dict 或元组中传递值,SQLite 会理解它应该使用它们作为数据库中的值

【讨论】:

    猜你喜欢
    • 2021-05-11
    • 2019-02-07
    • 2020-02-06
    • 2019-01-12
    • 2014-05-17
    • 2018-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多