Failing to sync collection from Python script

I have a few python scripts for manipulating my decks which work great but I’d also like to be able to sync my collection without starting up the desktop app. I wrote a simple little funciton like this which gets the SyncAuth object and passes it to Collection.sync_collection, like so:

def sync_deck():
    auth = col.sync_login("username", "password")
    col.sync_collection(auth)

However I get this exception:
…\env\lib\site-packages\anki_backend_init_.py", line 131, in _run_command
raise backend_exception_to_pylib(err)
anki.errors.DBError: DbError { info: “SqliteFailure(Error { code: Unknown, extended_code: 1 }, Some(“cannot start a transaction within a transaction”))”, kind: Other }

Any ideas why this is the case, have I missed a key enabling step?

Thank you,

Ben

Hi,
I have solved this one myself.

    col.save(trx=False)
    col.sync_collection(auth)

The error seemed to be something to do with transactions and sure enough the above modification to the code fixed it.

2 Likes

You should also save and re-use the auth info instead of authenticating each time, as you may end up being inadvertently blocked by doing it too frequently.

2 Likes

thanks have implemented this as well