Pacharapol Withayasakpunt Pacharapol Withayasakpunt
Mon, July 29, 2019
Creating and editing Anki's *.apkg and *.anki2 via SQLite

Creating and editing Anki's *.apkg and *.anki2 via SQLite

*.apkg and *.anki2 file structure is very simple, but with some quirks of incompleteness.

*.apkg file structure is a zip of at least two files.

.
├── example
│   ├── example.anki2
│   ├── media
│   ├── 1  # Media files with the names masked as numbers
│   ├── 2
│   ├── 3
|   └── ...
└── example.apkg

*.anki2 is a SQLite file with foreign key disabled, and the usage of some JSON schemas instead of some tables

Also, *.anki2 is used internally at os.path.join(appdirs.user_data_dir('Anki2'), 'User 1', 'collection.anki2'), so editing the SQLite there will also edit the database.

The media file is a text file of at least a string of {}, which is actually a dictionary of keys -- stringified int; and values -- filenames.

I created a PyPI package is ease the editing of this SQLite (and text file), powered by Peewee ORM.

https://github.com/patarapolw/ankisync2