可以在循环检查数据库时更改数据库吗?sqlite3

huangapple go评论60阅读模式
英文:

Can I change the DB while the cycle is checking it? sqlite3

问题

我想知道在从数据库每10秒获取数据的循环运行时是否可以更改数据库。库是aiosqlite。

循环的代码:

db = await aiosqlite.connect("/main.db")
while True:
    await asyncio.sleep(10.5)
    ti = await db.execute_fetchall("SELECT delete_time FROM auction WHERE id = ?", (id,))
    print("来自数据库的检查", ti[0][0])
    if float(ti[0][0]) > dt.now().timestamp():
        continue
    else:
        return True

更改的代码:

db = await aiosqlite.connect("/main.db")

timing = await db.execute_fetchall("SELECT delete_time FROM auction WHERE id = ?", (id,))
print("来自数据库", timing[0][0])

timing = float(timing[0][0]) + 60
print("已编辑", timing)

await db.execute("UPDATE auction SET delete_time = ? WHERE id = ?", (timing, id,))
await db.close()

看起来没有错误,但在数据库中什么都没有改变(当我写入命令时,数据库才会发生变化)。

英文:

I'm interested to know if I can change the DB while the loop is running that takes data from the database every 10 seconds? The library is aiosqlite.

The code of the loop:

    db = await aiosqlite.connect("/main.db")
    while True:
        await asyncio.sleep(10.5)
        ti = await db.execute_fetchall("SELECT delete_time FROM auction WHERE id = ?", (id,))
        print("from db check", ti[0][0])
        if float(ti[0][0]) > dt.now().timestamp():
            continue
        else:
            return True

Change code:

    db = await aiosqlite.connect("/main.db")

    timing = await db.execute_fetchall("SELECT delete_time FROM auction WHERE id = ?", (id,))
    print("from db", timing[0][0])

    timing = float(timing[0][0]) + 60
    print("edited", timing)

    await db.execute("UPDATE auction SET delete_time = ? WHERE id = ?", (timing, id,))
    await db.close()

There seem to be no errors, but nothing changes in the DB (the DB changes when I write a command).

答案1

得分: 0

你需要添加 await db.commit() 以便将更改持久化。

    db = await aiosqlite.connect("/main.db")

    timing = await db.execute_fetchall("SELECT delete_time FROM auction WHERE id = ?", (id,))
    print("from db", timing[0][0])

    timing = float(timing[0][0]) + 60
    print("edited", timing)

    await db.execute("UPDATE auction SET delete_time = ? WHERE id = ?", (timing, id,))
    await db.commit()  # 持久化更改
    await db.close()
英文:

You're missing a await db.commit() if you want to persist the changes.

    db = await aiosqlite.connect("/main.db")

    timing = await db.execute_fetchall("SELECT delete_time FROM auction WHERE id = ?", (id,))
    print("from db", timing[0][0])

    timing = float(timing[0][0]) + 60
    print("edited", timing)

    await db.execute("UPDATE auction SET delete_time = ? WHERE id = ?", (timing, id,))
    await db.commit()  # persist changes
    await db.close()

huangapple
  • 本文由 发表于 2023年2月10日 16:39:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408672.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定