Skip to content

Anki2 database documentation

nicolas-raoul edited this page Apr 8, 2013 · 5 revisions

Anki database structure

Anki uses one single sqlite database to store information of multiple decks, templates, fields and cards. This file can be found inside the Anki package file (.apkg file) with the extension anki2.

Extracting example.apkg we have the following structure.

.
├── example
│   ├── example.anki2
│   └── media
└── example.apkg

In linux sqliteman can be used to read and modify the .anki2 files.

Terminology

Anki contains bascially the following types:

  1. Cards
  2. Decks
  3. Notes
  4. Templates
  5. Collection

More information on what these represent are clearly explained in http://ankisrs.net/docs/manual.html#basics.

Tables

Anki database structure contains the following tables

col

The table represents collection.
TODO Can it have multiple collection?? What does multiple collection mean?

The structure is given below:

table|col|col|2|CREATE TABLE col (
    id              integer primary key,
    crt             integer not null,
    mod             integer not null,
    scm             integer not null,
    ver             integer not null,
    dty             integer not null,
    usn             integer not null,
    ls              integer not null,
    conf            text not null,
    models          text not null,
    decks           text not null,
    dconf           text not null,
    tags            text not null
)

Two important fields here are models and decks. A model defines how cards are generated from notes. More on this will be explained below. Also, each deck is a set of different cards. TODO link to deck section.

TODO Put image here

notes

A note is a piece of information that will generate a card. Notice that a note itself is not the card.

Example: When learning chemistry, we have this information:

Name: oxygen
Symbol: O
Number: 8

This piece of information can be used to generate multiple cards.

Card 1

Q: Chemical symbol for oxygen?
A: O

Card 2

Q: Chemical number for oxygen?
A: 8

Card 3

Q: Chemical name for O?
A: oxygen

A note can have two or more fields. In our example above we had three fields. The cards however will always have two sides. Front and Back.

Table structure for notes is given below:

table|notes|notes|3|CREATE TABLE notes (
    id              integer primary key,   /* 0 */
    guid            text not null,         /* 1 */
    mid             integer not null,      /* 2 */
    mod             integer not null,      /* 3 */
    usn             integer not null,      /* 4 */
    tags            text not null,         /* 5 */
    flds            text not null,         /* 6 */
    sfld            integer not null,      /* 7 */
    csum            integer not null,      /* 8 */
    flags           integer not null,      /* 9 */
    data            text not null          /* 10 */
)

cards

A card is generated when a note is combined with one of the model. A note contains facts for different fields. It contains no information on how to generate the cards. A model contains templates and each template contains how to take the fields in the note and show it as question side (also called front side) and answer side (also called back side).

In our example above, the model used contains three templates.

Template 1

Q: Chemical symbol for {{Name}}?
A: {{Symbol}}

Template 2

Q: Chemical number for {{Name}}?
A: {{Number}}

Template 3

Q: Chemical name for {{Symbol}}?
A: {{Name}}

So with these templates, we have three different cards for each note under our particular model.

Table structure for cards:

table|cards|cards|4|CREATE TABLE cards (
    id              integer primary key,   /* 0 */
    nid             integer not null,      /* 1 */
    did             integer not null,      /* 2 Deck ID. Every deck has its own did. */
    ord             integer not null,      /* 3 */
    mod             integer not null,      /* 4 */
    usn             integer not null,      /* 5 */
    type            integer not null,      /* 6 */
    queue           integer not null,      /* 7 */
    due             integer not null,      /* 8 */
    ivl             integer not null,      /* 9 */
    factor          integer not null,      /* 10 */
    reps            integer not null,      /* 11 */
    lapses          integer not null,      /* 12 */
    left            integer not null,      /* 13 */
    odue            integer not null,      /* 14 */
    odid            integer not null,      /* 15 Original deck ID. A filtered deck also has its own did. Each card inside a filtered deck has a reference to the deck it came from in odid. A card NOT in a filtered will have an odid of 0. */
    flags           integer not null,      /* 16 */
    data            text not null          /* 17 */
)

revlog

TODO

graves

TODO

ER Diagram of the whole structure

ER Diagram of the tables

JSON Data structures

Anki data format relies on JSON to store the data. Example: col.models, col.decks are in JSON format.

JSON Structure of the data

Unfinished

  1. TODO what does mid, usn values mean?
  2. TODO document the generic fields