Home > database >  Updating SQL table with discord.py
Updating SQL table with discord.py

Time:12-03

so I've got the "time_estimations_4" table where I want to update a value on the "Mod_on" table

problem: keeps the Mod_on column 0 instead of updating it to 1

extra info: printing out gives None

Here's the table creation:

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 
)'''

Here's the updating:

@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!" )

Anyone see possible problems?

CodePudding user response:

It appears as if you are never executing your query that is meant to update the Mod_on column, you create your sql2 variable with the query but then do nothing with it.

You should also get in the habit of building parameterized queries instead of interpolating the author id as it can make you prone to an SQL injection attack, although in this case an author id can only ever be an integer.

  • Related