Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Asyncpg INSERT查询时间戳返回错误语法错误在或接近;18“;_Python_Postgresql_Discord.py_Asyncpg - Fatal编程技术网

Python Asyncpg INSERT查询时间戳返回错误语法错误在或接近;18“;

Python Asyncpg INSERT查询时间戳返回错误语法错误在或接近;18“;,python,postgresql,discord.py,asyncpg,Python,Postgresql,Discord.py,Asyncpg,嗨,我在尝试添加时间戳时遇到了一个错误。我一直收到错误asyncpg.exceptions.PostgresSyntaxError:syntax错误在或接近“18”,这一行end\u date=duration+dt.datetime.now(bst)#timestamp似乎是罪魁祸首,但我不确定为什么会出现这种情况 以下是我的工作内容: if time is not None: conn = await asyncpg.connect(DATABASE_URL)

嗨,我在尝试添加时间戳时遇到了一个错误。我一直收到错误
asyncpg.exceptions.PostgresSyntaxError:syntax错误在或接近“18”
,这一行
end\u date=duration+dt.datetime.now(bst)#timestamp
似乎是罪魁祸首,但我不确定为什么会出现这种情况

以下是我的工作内容:

    if time is not None:
        conn = await asyncpg.connect(DATABASE_URL)
        async with conn.transaction():
            await conn.fetch(f"SELECT time FROM blacklist WHERE username={member.id}")
        
            duration = find_date(time)
            bst = pytz.timezone('Europe/London')
            end_date = duration + dt.datetime.now(bst) # timestamp
            fmt_date = end_date.strftime("%#d %b %Y, at %I:%M%p")
            await conn.fetch(f"UPDATE blacklist SET time={end_date} WHERE username={member.id}")
        
            await member.add_roles(restricted, reason=f'Restricted role added by {author.name}')
            await member.remove_roles(members)
            
            await conn.close()

            msg = f"{member} has been restricted until {fmt_date}."
            embed = discord.Embed(title="Restricted", description=msg, colour=author.color)
            await ctx.send(embed=embed)
            return

在处理SQL查询时,您没有在
f-strings
中传递参数,
asyncpg
中查询参数的语法是
$n
,而且我看到您正在使用
连接.fetch
方法,但您只是在更新表,我建议您使用
连接.execute

您的代码已修复:

end_date=#应该是datetime.datetime实例
等待连接执行(“”)
更新黑名单
设定时间=$1
其中username=$2
“”,结束日期,成员id)
消除时区意识

end_date=#`datetime.datetime`实例
naive=结束日期。替换(tzinfo=无)
等待连接执行(“”)
更新黑名单
设定时间=$1
其中username=$2
“”,朴素,成员.id)
参考资料:

PS:不要每次使用新连接时都创建新连接,通常的做法是使用一个长期数据库连接:基础连接已关闭不确定是否是相关错误。问题可能是您正在上下文管理器内关闭连接,将conn.close调用放在其外部
wait conn.execute(““更新黑名单设置时间=$1,其中username=$2”“,naive,member.id)
似乎不起作用。未向用户名或时间列添加任何内容。您正在更新时间,是否确定未添加任何内容?任何错误/回溯?结果是导致问题的discord.py
on_member_update
事件。