'how to delete a single specific value within a repl database?
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
Solution 1:[1]
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.
Solution 2:[2]
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
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | tdy |
Solution 2 |