【问题标题】:discord.py economy adding and subtracting into databasediscord.py 经济加减到数据库
【发布时间】: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


【解决方案1】:

您必须将犯罪函数的结果和存储在数据库中的钱相加。这样做购买使用 SELECT 语句在数据库中获取资金并将其转换为整数。然后添加它并使用 UPDATE 语句最终更新货币。

例子:

money = random.randrange(-150, 400)
db = sqlite3.connect('main.sqlite')
cursor = db.cursor()
cursor.execute(f"SELECT wallet FROM economy WHERE user_id = {ctx.author.id}")
OldMoney = int(cursor[0]["wallet"])
NewMoney = OldMoney + money
sql = f"UPDATE economy SET wallet = {NewMoney} WHERE user_id = {ctx.author.id}"
cursor.execute(sql)
cursor.close()
cursor.commit()
db.close()

【讨论】:

  • 最好在 SQL 查询中始终参数化 Python 值。 F-strings 只是不安全的字符串插值的新形式。见SQLite3 parameter docs
【解决方案2】:

考虑在开始查询中捕获wallet 信息(并在使用参数时保持一致):

cursor.execute("SELECT user_id, wallet FROM economy WHERE guild_id = ?", ctx.guild.id)
result = cursor.fetchone()

最后,在钱包里加钱:

sql = ("UPDATE economy SET wallet = ? WHERE user_id = ? AND guild_id = ?")
var = (result[1] + money, ctx.author.id, ctx.guild.id)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    • 1970-01-01
    • 2021-08-11
    • 2023-03-14
    • 2022-06-13
    • 2021-11-08
    • 2020-09-13
    相关资源
    最近更新 更多