如何让更新命令工作?上面脚本的其余部分有效,但更新失败

How do I get the update command to work? The rest of the script above works but update fails(如何让更新命令工作?上面脚本的其余部分有效,但更新失败)
本文介绍了如何让更新命令工作?上面脚本的其余部分有效,但更新失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@commands.command()
@commands.cooldown(1, 8, commands.BucketType.user)
async def Beg(self, ctx):
    possibility = randint(1, 10)
    if possibility == 5:
        await ctx.send("I'll just give you this <:DiscordError:879192336960196619>")
    else:
        amount = randint(25, 175)
        outcomes = [f"If you're going to keep begging me I guess I'll give you ${amount}", f"How about you get a job for yourself? But I guess I'll give you ${amount}", f"I'll give you ${amount} but you owe me cuddles after this!", f"Let me see what's in my wallet.. Oh it's ${amount}! Take it!"]
        outcomes1 = random.choice(outcomes)
        await ctx.send(outcomes1)
        db = sqlite3.connect("main.sqlite")
        cursor = db.cursor()
        result = cursor.fetchone()
        sql = f"UPDATE main SET money = * WHERE member_id = {ctx.message.author.id}"
        val = (result[1] + amount)
        cursor.execute(sql, val)
        db.commit()
        await ctx.send("Sent!")
        cursor.close()
        db.close()

正如我所说,更新命令不起作用.结果消息工作正常.它已发送,没有任何更新.

As I said, the update command doesn't work. The outcome messages works fine. It get's sent, nothing get's updated.

推荐答案

作为最佳实践,我建议将所有 ctx 确认消息移到函数末尾.我更喜欢这种方式,因为它有助于作为一种调试方式(如果前面的所有步骤都成功,则会发送确认消息).

As a best practice, I recommend moving all ctx confirmation messages to the end of the function. I prefer it that way, because it'll help serve as a sort of debugging (if all prior steps are successful, then a confirmation message is sent).

我建议删除 cursor.close() 因为该方法不适用于光标对象.您打开和关闭数据库连接,而不是游标.

I recommend removing cursor.close() as that method isn't applicable to the cursor object. You open and close the database connection, not the cursor.

result[1] 也应该是 result[0].在不知道您的数据库是如何设置的情况下,在尝试对其进行数学运算之前确保 result[0] 是一个整数也是值得的.

result[1] should be result[0], as well. Without knowing how your db is set up, it also might be worthwhile to ensure that result[0] is an integer before trying to do math on it.

这篇关于如何让更新命令工作?上面脚本的其余部分有效,但更新失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
FastAPI + Tortoise ORM + FastAPI Users (Python) - Relationship - Many To Many(FastAPI+Tortoise ORM+FastAPI用户(Python)-关系-多对多)
Inserting NaN value into MySQL Database(将NaN值插入MySQL数据库)
Window functions not working in pd.read_sql; Its shows error(窗口函数在pd.read_sql中不起作用;它显示错误)
(Closed) Leaflet.js: How I can Do Editing Geometry On Specific Object I Select Only?((已关闭)Leaflet.js:如何仅在我选择的特定对象上编辑几何图形?)
in sqlite update trigger with multiple if/Case Conditions(在具有多个IF/CASE条件的SQLite UPDATE触发器中)