Updated SQLite but doesn't sync changes to AnkiWeb

Sooo I programmatically updated a bunch of cards in my deck using Python, which worked great on my Desktop.

Only problem is it doesn’t sync the changes to AnkiWeb (for use on my mobile).

I think I’m forgetting to update some fields to say that this note now needs to be updated.

Currently this is how I’m updating a note:

UPDATE notes
SET flds = :flds, mod = unixepoch()
WHERE id = :id AND guid = :guid AND mid = :mid

Does anyone know how also mark that this code has been modified for AnkiWeb syncing?

The add-on guide warns that direct DB manipulations don’t sync automatically:

Try updating the collection’s modification time (The mod column in col). I think this should work but I’ve not tested.

If your goal can be achieved using the anki module, I recommend using that instead: The 'anki' Module - Writing Anki Add-ons


Awesome, ty, I had to do some digging in the source code, but I figured out how to use the anki Python library! It’s working perfectly!

For whatever reason, I wasn’t able to find how to do this when searching the internet, or asking the LLM-- which lead me to dipping into the SQLite DB. Articles were wanting to steer me to solutions for problems I didn’t have, like using genanki to create offline decks. >:[

Anyway, here’s my script to update live Anki notes programmatically from python! It’s really fun to use from a Jupyter Notebook as well. And maybe snippet will help some poor soul if they try Googling “Manipulate Anki data from Python”.

Use Python to update live Anki data

import anki
import anki.collection
from pathlib import Path

# open anki collection (the app must be closed!!)
col = anki.collection.Collection(Path("~/Library/Application Support/Anki2/default/collection.anki2").expanduser().as_posix())

# get the notes & decks of interest
deck_id = 1694176094392
notes = [col.get_note(x) for x in col.find_notes(f"did:{deck_id}")]

# do some cool updates
for note in notes:
    note["Spanish"] = note["Spanish"].strip()  # i.e. remove leading/trailing whitespace!

# commit updates
print(f"Updated {len(notes)} notes.")

# close, and reopen Anki app

Just make sure to backup in the Anki app: Anki > File > Create Backup!!!

1 Like

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