Where is the temporary collection database stored while Anki is running?

I am trying to write a basic script that reads (and reads only) some collection metadata. I want it to be able to run while Anki is running.

col = Collection(f"{collection_filename}") can not be used while Anki is running (errors). So, I copy the collection to /tmp/ for the execution, since I only need to read. This looks something like:

from anki.collection import Collection
import os, shutil

USER = os.environ['USER']
collection_filename = "collection.anki2"
collection_path = f"/home/{USER}/.local/share/Anki2/User 1/{collection_filename}" 
shutil.copy(collection_path, "/tmp/")
col = Collection(f"/tmp/{collection_filename}")

The only problem now is that collection.anki2, stored in ~/.local/share/Anki2/User\ 1/, is only updated after Anki quits.

I had a look and couldn’t find any temporary collection database file anywhere that would be readable while Anki is running.

I am wondering if this is just kept in Anki’s runtime memory, or if there is an up-to-date DB file I could read while Anki is still running?

Thank you :slight_smile:

2 Likes

Anki uses SQLite’s WAL feature, so you have to consider the collection.anki2-wal file too. I’m not sure though if it’s safe to simply copy (the sqlite3-rsync utility is recommended for that), but I recommend giving it a try.

An alternative approach is to use AnkiConnect instead, if it has methods for the data you’re interested in.

3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.