【问题标题】:Updating SQL table with discord.py使用 discord.py 更新 SQL 表
【发布时间】:2022-01-09 06:27:43
【问题描述】:

所以我有“time_estimations_4”表,我想在其中更新“Mod_on”表上的值

问题: 保留 Mod_on 列 0 而不是将其更新为 1

额外信息: 打印出来没有

创建表如下:

sql = '''CREATE TABLE IF NOT EXISTS time_estimation_4(
    turn BIGINT primary key auto_increment,
    Mod_on int DEFAULT 0,
    mod_id BIGINT,
    mods_online BIGINT 
)'''

这里是更新:

@pro.command()
async def online(ctx):
    sql_check_user = f'''SELECT mod_id FROM time_estimation_4 WHERE mod_id = {ctx.author.id}'''
    cur.execute(sql_check_user)
    out_user = cur.fetchone()
    user = ctx.author.id
    if out_user == None:
        cur.execute('INSERT INTO time_estimation_4 (mod_id) VALUES (%s)', (user,))
        db.commit()
    sql1 = f'''SELECT Mod_on FROM time_estimation_4 WHERE mod_id = {ctx.author.id}'''
    cur.execute(sql)
    out = cur.fetchone()
    print(out)
    if out == 1:
        await ctx.send("mod's already online")
    elif out == 0:
        sql2 = f'''UPDATE time_estimation_4 SET Mod_on = 1 WHERE mod_id = {ctx.author.id}'''
        await ctx.send("Done! You won't be recieving notifications and time estimation will be longer!" )

有人看到可能的问题吗?

【问题讨论】:

    标签: python mysql sql discord discord.py


    【解决方案1】:

    您似乎从未执行过旨在更新 Mod_on 列的查询,您使用该查询创建了 sql2 变量,但随后什么也不做。

    您还应该养成构建参数化查询的习惯,而不是插入作者 ID,因为这会使您容易受到 SQL 注入攻击,尽管在这种情况下,作者 ID 只能是整数。

    【讨论】:

      猜你喜欢
      • 2020-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-31
      • 2013-11-12
      • 1970-01-01
      • 2013-09-09
      • 1970-01-01
      相关资源
      最近更新 更多