【发布时间】:2020-08-24 19:14:04
【问题描述】:
我正在使用 sqlite3 进行经济命令(我是 sqlite3 的新手)。我有犯罪命令从 -150(被捕)到 400 中选择一个随机数。但是当你运行命令时,它会将你的钱重置为你得到的任何东西。示例 数据库中有 330。您运行命令 >crime 并获得 150。您的余额从 330 变为 150 而不是 480。
@client.command()
@commands.cooldown(1, 3600, commands.BucketType.user)
async def crime(ctx):
money = random.randrange(-150, 400)
db = sqlite3.connect('main.sqlite')
cursor = db.cursor()
cursor.execute(f"SELECT user_id FROM economy WHERE guild_id = {ctx.guild.id}")
result = cursor.fetchone()
if result is None:
await ctx.send("Oh no! You do not have an account! You can create an account by typing command `>open_account`.")
else:
if money == 0:
await ctx.send("Dang... The person you robbed had no money on them")
elif money <= 0:
await ctx.send(f"You were caught!! You were fined ${money}.")
elif money > 5:
await ctx.send(f"Was it all worth it? You made ${money}.")
elif money > 15:
await ctx.send(f"I mean at least you stole ${money}.")
elif money > 30:
await ctx.send(f"You stole a phone and made ${money} off of it.")
elif money > 45:
await ctx.send(f"You stole ${money}. Nice!")
elif money > 60:
await ctx.send(f"You stole everything you could grab in a car. You sold it all for ${money}.")
elif money > 75:
await ctx.send(f"You broke into a house and stole a tv! You made ${money} off of it.")
elif money > 100:
await ctx.send(f"You stole a laptop and sold it for ${money}.")
elif money > 150:
await ctx.send(f"You robbed a dude for his new and expensive shoes. You made ${money}.")
elif money > 200:
await ctx.send(f"You pushed a kid off his bike. You were able to sell it for ${money}.")
elif money > 300:
await ctx.send(f"You stole a car and scraped it for ${money}.")
sql = ("UPDATE economy SET wallet = ? WHERE user_id = ? AND guild_id = ?")
var = (money, ctx.author.id, ctx.guild.id)
cursor.execute(sql, var)
db.commit()
cursor.close()
db.close()
如何将 150 添加到 300?
【问题讨论】:
-
你必须从数据库中
SELECT,在 Python 中添加/减去值,在数据库中添加/减去UPDATE新值。
标签: python sql python-3.x discord.py