-3

I am writing a discord bot with using SQLite and discord.py.

This is the command that causes the error:

@bot.command()
@commands.has_permissions(administrator=True)
async def set_ip(ctx, arg=None):
    if arg == None:
        await ctx.send("You must type the IP address next to the command!")
    elif arg.endswith('.aternos.me') == False:
        await ctx.send('IP must end with .aternos.me')
    elif ctx.guild.id == None:
        await ctx.send("This is a guild-only command!")
    else:
        ipas = None
        id = ctx.guild.id
        conn.execute(f'''DROP TABLE IF EXISTS guild_{id}''')
        conn.execute(f'''CREATE TABLE IF NOT EXISTS guild_{id} (
            ip TEXT NOT NULL
        )''')
        conn.execute(f'''INSERT INTO guild_{id} ("ip") VALUES ({arg})''')
        cursor = conn.execute(f'''SELECT ip FROM guild_{id}''')
        for row in cursor:
            ipas = row[0]
        if ipas == None:
            await ctx.send("Failed to set IP!")
            conn.execute(f'''DROP TABLE IF EXISTS guild_{id}''')
        else:
            await ctx.send(f"Your guild ip is now -> {ipas}")
            print("An ip has been set!")

I tried to create a table that if not exist with name of guild_(and the discord server id), and check that it is set or not

Error is:

OperationalError: no such column: (the arg)

SQLite throws this error and I can't figure it out, please help me.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
kagent263
  • 21
  • 5
  • 1
    Separate from your actual question, but why create a separate table for each guild? It seems likely that a better design would have a single `guild` table. In general, I wouldn't expect an application to be creating and dropping tables very often. – EdmCoff Nov 16 '22 at 22:28
  • Does this answer your question? [How to use variables in SQL statement in Python?](https://stackoverflow.com/questions/902408/how-to-use-variables-in-sql-statement-in-python) – PChemGuy Nov 17 '22 at 04:40
  • i tried to did what edmcoff said but it gives the same error – kagent263 Nov 17 '22 at 10:58

1 Answers1

0

You are passing the column name as a string:

conn.execute(f'''INSERT INTO guild_{id} ("ip") VALUES ({arg})''')

should be:

conn.execute(f'''INSERT INTO guild_{id} (ip) VALUES ({arg})''')
Gerballi
  • 441
  • 5