1

Im making a discord python bot using repl's database function and i cant figure out how to delete only one value from the database.

@client.command()
async def addvalue(ctx, arg):
 db[str(ctx.author.id)] = [arg]
 await ctx.send("value added")

@client.command()
async def removevalue(ctx, arg):
 db[str(ctx.author.id)] = db[str(ctx.author.id)] - arg #ik this is a dumb way of trying this, this is just theory obv this wouldnt work
lolita
  • 43
  • 5

2 Answers2

2

What I had to do was find the index of the of the value that I want to be deleted:

async def removevalue(ctx, arg):
  c = 0
  for x in db[str(ctx.author.id)]:
    if str(arg) in db[str(ctx.author.id)][c]:
      del db[str(ctx.author.id)][c]
      await ctx.send("Value deleted")
    return
    else:
      c += 1

Note that I put str() around arg but I'm pretty sure that was useless. I was just trying everything to fix it.

tdy
  • 36,675
  • 19
  • 86
  • 83
lolita
  • 43
  • 5
1

I've never used it before but from looking at the example code it seems you can just use:

del db[str(ctx.author.id)][index]

From the docs it's just a key/value store so it seems it acts the same as any dict.

Jab
  • 26,853
  • 21
  • 75
  • 114