pythonsqlitedisnake

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


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).


Solution

  • 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()