Python 3.x 数据库问题中的信息更新

Python 3.x 数据库问题中的信息更新,python-3.x,postgresql,discord.py,Python 3.x,Postgresql,Discord.py,您好,我正在尝试更新数据库中的某些条目,但在更新值中指定的正确信息时遇到问题 下面我的代码块显示了一个命令,允许用户给用户XP。我试图实现的是更新两个值val_1和val_2但是val_2xp amount,total_xp-amount在调用命令时从指定的amount中错误地扣除了更大的金额(数字)。例如当时!xp@user 5被调用时,它应该给所提到的@user5 xp,并从调用该命令的用户处获取5XP 以下是我的工作内容: async def xp(self, ctx, user: dis

您好,我正在尝试更新数据库中的某些条目,但在更新值中指定的正确信息时遇到问题

下面我的代码块显示了一个命令,允许用户给用户XP。我试图实现的是更新两个值
val_1
val_2
但是
val_2
xp amount,total_xp-amount
在调用命令时从指定的
amount
中错误地扣除了更大的金额(数字)。例如当时!xp@user 5被调用时,它应该给所提到的
@user
5 xp,并从调用该命令的用户处获取5XP

以下是我的工作内容:

async def xp(self, ctx, user: discord.Member, amount: int):

    conn = psycopg2.connect(DATABASE_URL, sslmode='require')
    cursor = conn.cursor()
    cursor.execute(f"SELECT lvl, xp, total_xp FROM levels WHERE guild_id = {ctx.guild.id} AND user_id = {user.id}")
    result2 = cursor.fetchone()
    xp = int(result2[1])
    total_xp = int(result2[2])

    if user.id == ctx.message.author.id:
        await ctx.send("You can't give yourself XP.")
        return

    if int(amount) > 50:
        await ctx.send("You can only give a maximum of 50 XP.")
        return

    #SQL 
    sql = ("UPDATE levels SET xp=%s, total_xp=%s WHERE guild_id=%s and user_id=%s")

    #The member recieving XP
    val_1 = (xp+amount, total_xp+amount, str(ctx.guild.id), str(user.id))
    cursor.execute(sql, val_1)

    #The member giving XP 
    val_2 = (xp-amount, total_xp-amount, str(ctx.guild.id), str(ctx.message.author.id))
    cursor.execute(sql, val_2)
    
    conn.commit()
    cursor.close()
    conn.close()

    #return something here
    await ctx.send(f"{ctx.message.author.name} has given {amount} XP to 
    {user.mention}.") 

非常感谢您的帮助

首先,您没有获得两个用户的全部xp,只有调用该命令的用户

之前:

cursor.execute(f"SELECT lvl, xp, total_xp FROM levels WHERE guild_id = {ctx.guild.id} AND user_id = {user.id}")
    result2 = cursor.fetchone()
    xp = int(result2[1])
    total_xp = int(result2[2])

    if user.id == ctx.message.author.id:
        await ctx.send("You can't give yourself XP.")
        return

    if int(amount) > 50:
        await ctx.send("You can only give a maximum of 50 XP.")
        return

    #SQL 
    sql = ("UPDATE levels SET xp=%s, total_xp=%s WHERE guild_id=%s and user_id=%s")

    #The member recieving XP
    val_1 = (xp+amount, total_xp+amount, str(ctx.guild.id), str(user.id))
    cursor.execute(sql, val_1)

    #The member giving XP 
    val_2 = (xp-amount, total_xp-amount, str(ctx.guild.id), str(ctx.message.author.id))
    cursor.execute(sql, val_2)
固定的:

cursor.execute(f"SELECT lvl, xp, total_xp FROM levels WHERE guild_id = {ctx.guild.id} AND user_id = {ctx.message.author.id}")
    result2 = cursor.fetchone()
    xp = int(result2[1])
    sender_total_xp = int(result2[2])

    if user.id == ctx.message.author.id:
        await ctx.send("You can't give yourself XP.")
        return

    if int(amount) > 50:
        await ctx.send("You can only give a maximum of 50 XP.")
        return

    cursor.execute(f"SELECT lvl, xp, total_xp FROM levels WHERE guild_id = {ctx.guild.id} AND user_id = {user.id}")

    result = cursor.fetchone()
    receiver_total_xp = int(result[2])

    #SQL 
    sql = ("UPDATE levels SET xp=%s, total_xp=%s WHERE guild_id=%s and user_id=%s")

    #The member recieving XP
    val_1 = (xp+amount, receiver_total_xp+amount, str(ctx.guild.id), str(user.id))
    cursor.execute(sql, val_1)

    #The member giving XP 
    val_2 = (xp-amount, sender_total_xp-amount, str(ctx.guild.id), str(ctx.message.author.id))
    cursor.execute(sql, val_2)
如您所见,您一直在从数据库请求不正确的数据,因为您使用的是user.id(目标id,即接收xp的人),而不是调用命令的人ctx.message.author.id


还有一个问题是您将发送方的xp设置为接收方的xp的-5。

有什么问题?您是否收到任何错误/回溯?结果如何?预期的结果是什么?请添加尽可能多的详细信息此问题是我的代码
val_2=(xp amount,total_xp-amount,str(ctx.guild.id),str(ctx.message.author.id))
中的这一行没有删除
xp amount,total_xp-amount,
的值大于命令调用的值。没有错误或回溯。