【问题标题】:how update a valor in sqlite3 in my discord bot如何在我的不和谐机器人中更新 sqlite3 中的值
【发布时间】:2021-02-05 12:47:28
【问题描述】:

我做了一个机器人不和谐,我创建了一个名为“Users.db”的 sql_base,我正在尝试修改 xp 案例,但我不知道该怎么做。

@bot.event
async def on_message(message):
    if message.author.bot:
        print("hopla")
        await bot.process_commands(message)

    else:
        xp = 1
        string_test = "UPDATE USERS set xp_ecrit = " + (( (xp_ecrit) + 1 + ))" where name=" + str(message.author) + ";"

        print(string_test)

我把我的问题放在括号里,我可以做一些类似 value = xp_ecrit where name = "example" 的事情吗?

感谢您的回答

【问题讨论】:

  • 您不能在字符串连接中进行数学运算和/或使用整数/浮点数。在字符串格式化之前计算新值,然后连接变量中数字的字符串表示形式 - 类似于"UPDATE..." + str(xp_ecrit) + "WHERE..."
  • 哦,好吧,你知道我怎样才能恢复价值,我搜索但我找不到如何做:value = xp_ecrit where id = 2(它的一个例子):s
  • 看起来您正在使用 SQLite,如果是这样,请查看他们的 CRUD 教程(创建、读取、更新和删除)。这是SQLite Python 文档。

标签: python python-3.x sqlite discord.py


【解决方案1】:

我已经完成了我的项目,但如果它对这个世界上的某个人有用的话。要更新 sqlite3 中的 valor,您必须这样做:

con = sqlite3.connect('Users.db')

with con:
            curr = con.cursor()
            sql = ("UPDATE USERS SET variable1 = ? where variable2 = ?")
            val = (var1, var2)
            curr.execute(sql, val)

祝你有美好的一天!

【讨论】:

    猜你喜欢
    • 2020-03-19
    • 1970-01-01
    • 2021-10-14
    • 2021-06-02
    • 2021-02-28
    • 2021-06-15
    • 2017-09-13
    • 2021-05-09
    • 1970-01-01
    相关资源
    最近更新 更多