Asyncpg INSERT query for timestamp returns ERROR syntax error at or near "18"
Asyncpg INSERT query for timestamp returns ERROR syntax error at or near "18"
您好,我在尝试添加时间戳时遇到错误。我不断收到错误 asyncpg.exceptions.PostgresSyntaxError: syntax error at or near "18"
,这一行 end_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
,我也看到了您正在使用 Connection.fetch
方法,但您只是在更新 table,我建议您使用 Connection.execute
您的代码已修复:
end_date = # Should be a datetime.datetime instance
await conn.execute("""
UPDATE blacklist
SET time =
WHERE username =
""", end_date, member.id)
删除时区感知
end_date = # `datetime.datetime` instance
naive = end_date.replace(tzinfo=None)
await conn.execute("""
UPDATE blacklist
SET time =
WHERE username =
""", naive, member.id)
参考文献:
PS:不要每次要用就新建一个连接,正常的做法是有一个长期的数据库连接
您好,我在尝试添加时间戳时遇到错误。我不断收到错误 asyncpg.exceptions.PostgresSyntaxError: syntax error at or near "18"
,这一行 end_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
,我也看到了您正在使用 Connection.fetch
方法,但您只是在更新 table,我建议您使用 Connection.execute
您的代码已修复:
end_date = # Should be a datetime.datetime instance
await conn.execute("""
UPDATE blacklist
SET time =
WHERE username =
""", end_date, member.id)
删除时区感知
end_date = # `datetime.datetime` instance
naive = end_date.replace(tzinfo=None)
await conn.execute("""
UPDATE blacklist
SET time =
WHERE username =
""", naive, member.id)
参考文献:
PS:不要每次要用就新建一个连接,正常的做法是有一个长期的数据库连接