【问题标题】:how to check if databases values are already exists如何检查数据库值是否已经存在
【发布时间】:2020-07-15 18:19:40
【问题描述】:

我正在创建一个不和谐的机器人来计算用户发送的消息数量,我刚开始,我想检查用户 ID 和公会 ID 是否已经插入,因为当我运行时并发送消息,它只是复制相同的公会 ID 和用户 ID。 我该如何解决?

这是我的代码:

@client.event
async def on_message(message):
    sql = ("INSERT INTO citizens (guild_id, user_id) VALUES (%s, %s)")
    val = (message.guild.id, message.author.id)
    mycursor.execute(sql, val)

    mydb.commit()

【问题讨论】:

    标签: python mysql discord discord.py


    【解决方案1】:

    您可以在 guild_id, user_id 上设置唯一约束并在 mycursor.execute(sql, val) 上添加 try catch。

    【讨论】:

      【解决方案2】:

      如果值已经存在,您可以先在数据库中进行查询检查,然后更新现有值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-10
        • 2017-02-21
        • 1970-01-01
        • 2012-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多