【问题标题】:SQLite database not updating with using PythonSQLite 数据库不使用 Python 更新
【发布时间】:2021-07-27 09:33:18
【问题描述】:

我认为我的数据库更新有误?它没有给我一个错误,所以这可能是我忘记做或没有注意到的事情。无论如何,我超级难过,我正在看的教程没有这个问题。

提前感谢所有帮助!

@bot.command(aliases = ['gen' , 'newacc'])
async def open_account(tendant):

    db = sqlite3.connect('leveling.sqlite')
    cursor = db.cursor()

    sqlupdate = (f'INSERT INTO leveling(user_id,xp, local_xp) VALUES(?,?,?)')

    val = (tendant.author.id, 0, 0)

    result = cursor.fetchall()
    await tendant.channel.send(result)
    cursor.execute(sqlupdate, val)
    db.close()

bot.run(token)

【问题讨论】:

  • 我认为,您没有提交更改。试试:db.commit()db.close() 上面添加这个

标签: python database sqlite discord.py


【解决方案1】:

您需要commit您的更改:

async def open_account(tendant):
    db = sqlite3.connect('leveling.sqlite')
    cursor = db.cursor()

    val = (tendant.author.id, 0, 0)
    cursor.execute('INSERT INTO leveling (user_id, xp, local_xp) VALUES(?, ?, ?)', val)
    
    # Save (commit) the changes
    db.commit()
    
    # We can close the connection if we are done with it.
    # Just be sure any changes have been committed or they will be lost.
    db.close()

见:https://docs.python.org/3/library/sqlite3.html

【讨论】:

  • 嘿伙计,谢谢!我试图找到这方面的文档,但由于某种原因我无法找到它们。因此,感谢您的回答并感谢您提供文档链接!
猜你喜欢
  • 1970-01-01
  • 2020-03-31
  • 2021-04-07
  • 2021-12-19
  • 1970-01-01
  • 2013-09-13
  • 1970-01-01
  • 1970-01-01
  • 2021-08-06
相关资源
最近更新 更多