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.
这篇关于如何让更新命令工作?上面脚本的其余部分有效,但更新失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何让更新命令工作?上面脚本的其余部分有效,但更新失败
基础教程推荐
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 带更新的 sqlite CTE 2022-01-01
