FANDOM


How to create a sample APKG fileEdit

  1. Install the latest Anki Desktop
  2. Create a sample deck
  3. File > Export
  4. Uncheck the checkboxes
  5. Save
  6. Unzip the .apkg file that has been generated
  7. Open the collection.anki2 with SQLiteBrowser

Sample collection.anki2Edit

The following is for a deck called "tatoeba" containing 2 notes: Bonjour→Hello, Merci→Thank you.

PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
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
);
INSERT INTO col VALUES(1,1332961200,1398130163295,1398130163168,11,0,0,0,'{"nextPos": 1, "estTimes": true, "activeDecks": [1], "sortType": "noteFld", "timeLim": 0, "sortBackwards": false, "addToCur": true, "curDeck": 1, "newBury": true, "newSpread": 0, "dueCounts": true, "curModel": "1398130163168", "collapseTime": 1200}','{"1342697561419": {"vers": [], "name": "Basic", "tags": [], "did": 1398130078204, "usn": -1, "req": [[0, "all", [0]]], "flds": [{"name": "Front", "rtl": false, "sticky": false, "media": [], "ord": 0, "font": "Arial", "size": 12}, {"name": "Back", "rtl": false, "sticky": false, "media": [], "ord": 1, "font": "Arial", "size": 12}], "sortf": 0, "latexPre": "\\documentclass[12pt]{article}\n\\special{papersize=3in,5in}\n\\usepackage{amssymb,amsmath}\n\\pagestyle{empty}\n\\setlength{\\parindent}{0in}\n\\begin{document}\n", "tmpls": [{"name": "Forward", "qfmt": "Template:Front", "did": null, "bafmt": "", "afmt": "Template:FrontSide\n\n
<hr id=answer/>\n\n{{Back}}", "ord": 0, "bqfmt": ""}], "latexPost": "\\end{document}", "type": 0, "id": 1342697561419, "css": ".card {\n font-family: arial;\n font-size: 30px;\n text-align: center;\n color: black;\n background-color: white;\n}\n\n.card1 { background-color: #FFFFFF; }", "mod": 1398130117}}','{"1": {"desc": "", "name": "Default", "extendRev": 50, "usn": 0, "collapsed": false, "newToday": [0, 0], "timeToday": [0, 0], "dyn": 0, "extendNew": 10, "conf": 1, "revToday": [0, 0], "lrnToday": [0, 0], "id": 1, "mod": 1398130160}, "1398130078204": {"desc": "", "name": "tatoeba", "extendRev": 50, "usn": -1, "collapsed": false, "newToday": [754, 0], "timeToday": [754, 0], "dyn": 0, "extendNew": 10, "conf": 1, "revToday": [754, 0], "lrnToday": [754, 0], "id": 1398130078204, "mod": 1398130140}}','{"1": {"name": "Default", "replayq": true, "lapse": {"leechFails": 8, "minInt": 1, "delays": [10], "leechAction": 0, "mult": 0}, "rev": {"perDay": 100, "fuzz": 0.05, "ivlFct": 1, "maxIvl": 36500, "ease4": 1.3, "bury": true, "minSpace": 1}, "timer": 0, "maxTaken": 60, "usn": 0, "new": {"perDay": 20, "delays": [1, 10], "separate": true, "ints": [1, 4, 7], "initialFactor": 2500, "bury": true, "order": 1}, "mod": 0, "id": 1, "autoplay": true}}','{}');
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 */
);
INSERT INTO notes VALUES(1398130088495,'Ot0!xywPWG',1342697561419,1398130110,-1,,'Bonjour�Hello','Bonjour',4077833205,0,);
INSERT INTO notes VALUES(1398130111274,'OQxYbRc]Dm',1342697561419,1398130117,-1,,'Merci�Thank you','Merci',1273459409,0,);
CREATE TABLE cards (
    id              integer primary key,   /* 0 */
    nid             integer not null,      /* 1 */
    did             integer not null,      /* 2 */
    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 */
    flags           integer not null,      /* 16 */
    data            text not null          /* 17 */
);
INSERT INTO cards VALUES(1398130110964,1398130088495,1398130078204,0,1398130110,-1,0,0,484332854,0,0,0,0,0,0,0,0,);
INSERT INTO cards VALUES(1398130117922,1398130111274,1398130078204,0,1398130117,-1,0,0,353754516,0,0,0,0,0,0,0,0,);
CREATE TABLE revlog (
    id              integer primary key,
    cid             integer not null,
    usn             integer not null,
    ease            integer not null,
    ivl             integer not null,
    lastIvl         integer not null,
    factor          integer not null,
    time            integer not null,
    type            integer not null
);
CREATE TABLE graves (
    usn             integer not null,
    oid             integer not null,
    type            integer not null
);
ANALYZE sqlite_master;
INSERT INTO "sqlite_stat1" VALUES('col',NULL,'1');
CREATE INDEX ix_notes_usn on notes (usn);
CREATE INDEX ix_cards_usn on cards (usn);
CREATE INDEX ix_revlog_usn on revlog (usn);
CREATE INDEX ix_cards_nid on cards (nid);
CREATE INDEX ix_cards_sched on cards (did, queue, due);
CREATE INDEX ix_revlog_cid on revlog (cid);
CREATE INDEX ix_notes_csum on notes (csum);
COMMIT;

Creating an APKG file from scratchEdit

Let's say you have a list of basic French-English vocabulary, and want to generate an APKG file from this list, programmatically. The general workflow is:

  1. Use the sample SQL code above as a template.
  2. Replace the identifiers, so that they are unique and do not conflict with other decks.
  3. For each French-English word couple, create both a note and its associated card.
  4. Use the sqlite3 command to convert the SQL source code file into an SQLite database file.
  5. Add a file for the media (no media so it will be a very short file).
  6. Zip the whole and rename as .apkg

Creating the collectionEdit

  1. id: 1 - an integer identifier. We can leave it untouched.
  2. crt: 1332961200
  3. mod: 1398130163295
  4. scm: 1398130163168
  5. ver: 11 - We can leave it untouched.
  6. dty: 0 - We can leave it untouched.
  7. usn: 0 - We can leave it untouched.
  8. ls: 0 - We can leave it untouched.
  9. conf: <see below> - configuration of the collection. We can leave it untouched.
  10. models <see below> - models available in the collection. For our example, we just need a Basic model so we can leave it untouched.
  11. decks <see below> - Decks of the collection. Leave the first (default deck) untouched, but for the second deck, modify the deck name ("tatoeba" in the sample) and generate the deck id as a random integer.
  12. dconf <see below> - Configuration of each deck. We can leave it untouched.
  13. tags - We can leave it untouched.

Field conf:

{
   "nextPos":1,
   "estTimes":true,
   "activeDecks":[
      1
   ],
   "sortType":"noteFld",
   "timeLim":0,
   "sortBackwards":false,
   "addToCur":true,
   "curDeck":1,
   "newBury":true,
   "newSpread":0,
   "dueCounts":true,
   "curModel":"1398130163168",
   "collapseTime":1200
}

Field models:

{
   "1342697561419":{
      "vers":[

      ],
      "name":"Basic",
      "tags":[

      ],
      "did":1398130078204,
      "usn":-1,
      "req":[
         [
            0,
            "all",
            [
               0
            ]
         ]
      ],
      "flds":[
         {
            "name":"Front",
            "rtl":false,
            "sticky":false,
            "media":[

            ],
            "ord":0,
            "font":"Arial",
            "size":12
         },
         {
            "name":"Back",
            "rtl":false,
            "sticky":false,
            "media":[

            ],
            "ord":1,
            "font":"Arial",
            "size":12
         }
      ],
      "sortf":0,
      "latexPre":"\\documentclass[12pt]{article}\n\\special{papersize=3in,5in}\n\\usepackage{amssymb,amsmath}\n\\pagestyle{empty}\n\\setlength{\\parindent}{0in}\n\\begin{document}\n",
      "tmpls":[
         {
            "name":"Forward",
            "qfmt":"{{Front}}",
            "did":null,
            "bafmt":"",
            "afmt":"{{FrontSide}}\n\n
              <hr id=answer/>\n\n{{Back}}",
            "ord":0,
            "bqfmt":""
         }
      ],
      "latexPost":"\\end{document}",
      "type":0,
      "id":1342697561419,
      "css":".card {\n font-family: arial;\n font-size: 30px;\n text-align: center;\n color: black;\n white;\n}\n\n.card1 { #FFFFFF; }",
      "mod":1398130117
   }
}

Field decks:

{
   "1":{
      "desc":"",
      "name":"Default",
      "extendRev":50,
      "usn":0,
      "collapsed":false,
      "newToday":[
         0,
         0
      ],
      "timeToday":[
         0,
         0
      ],
      "dyn":0,
      "extendNew":10,
      "conf":1,
      "revToday":[
         0,
         0
      ],
      "lrnToday":[
         0,
         0
      ],
      "id":1,
      "mod":1398130160
   },
   "1398130078204":{
      "desc":"",
      "name":"tatoeba",
      "extendRev":50,
      "usn":-1,
      "collapsed":false,
      "newToday":[
         754,
         0
      ],
      "timeToday":[
         754,
         0
      ],
      "dyn":0,
      "extendNew":10,
      "conf":1,
      "revToday":[
         754,
         0
      ],
      "lrnToday":[
         754,
         0
      ],
      "id":1398130078204,
      "mod":1398130140
   }
}

Field dconf:

{
   "1":{
      "name":"Default",
      "replayq":true,
      "lapse":{
         "leechFails":8,
         "minInt":1,
         "delays":[
            10
         ],
         "leechAction":0,
         "mult":0
      },
      "rev":{
         "perDay":100,
         "fuzz":0.05,
         "ivlFct":1,
         "maxIvl":36500,
         "ease4":1.3,
         "bury":true,
         "minSpace":1
      },
      "timer":0,
      "maxTaken":60,
      "usn":0,
      "new":{
         "perDay":20,
         "delays":[
            1,
            10
         ],
         "separate":true,
         "ints":[
            1,
            4,
            7
         ],
         "initialFactor":2500,
         "bury":true,
         "order":1
      },
      "mod":0,
      "id":1,
      "autoplay":true
   }
}

Creating a note and associated cardEdit

If you are not familiar with it already, learn about the concept of cards and notes: http://ankisrs.net/docs/manual.html#cards First create the note:

INSERT INTO notes VALUES(1398130088495,'Ot0!xywPWG',1342697561419,1398130110,-1,,'Bonjour�Hello','Bonjour',4077833205,0,);

Fields:

  1. id: 1398130088495 - The note id, generate it randomly.
  2. guid: 'Ot0!xywPWG' - a GUID identifier, generate it randomly.
  3. mid: 1342697561419 - Identifier of the model, use the one found in the models JSON section.
  4. mod: 1398130110 - Replace with current time (seconds since 1970).
  5. usn: -1 - We can leave it untouched.
  6. tags: - Tags, visible to the user, which can be used to filter cards (e.g. "verb"). We can leave it untouched (empty string).
  7. flds: 'Bonjour�Hello' - Card content, front and back, separated by \x1f char.
  8. sfld: 'Bonjour' - Card front content without html (first part of flds, filtered).
  9. csum: 4077833205 - A string SHA1 checksum of sfld, limited to 8 digits. PHP: (int)(hexdec(getFirstNchars(sha1($sfld), 8)));
  10. flags: 0 - We can leave it untouched.
  11. data: - We can leave it untouched.

Then create the associated card:

INSERT INTO cards VALUES(1398130110964,1398130088495,1398130078204,0,1398130110,-1,0,0,484332854,0,0,0,0,0,0,0,0,);

Fields:

  1. id: 1398130110964 - The card id, generate it randomly.
  2. nid: 1398130088495 - The note id this card is associated with.
  3. did: 1398130078204 - The deck id this card is associated with.
  4. ord: 0 - We can leave it untouched.
  5. mod: 1398130110 - Same as note's mod field
  6. usn: -1 - We can leave it untouched.
  7. type: 0 - We can leave it untouched.
  8. queue: 0 - We can leave it untouched.
  9. due: 484332854 - We can leave it untouched.
  10. ivl: 0 - We can leave it untouched.
  11. factor: 0 - We can leave it untouched.
  12. reps: 0 - We can leave it untouched.
  13. lapses: 0 - We can leave it untouched.
  14. left: 0 - We can leave it untouched.
  15. odue: 0 - We can leave it untouched.
  16. odid: 0 - We can leave it untouched.
  17. flags: 0 - We can leave it untouched.
  18. data: - We can leave it untouched.

Media Formatting Edit

If the deck contains media, the media file (which is just text) within the APKG archive will contain a list of media files in curly braces, such as

{"1": "ponyoreview.jpg", "0": "07Dads.JPG"}

where in this case there are two included images.

Additionally within the APKG will be files named with sequential integers corresponding to the references in the media file. In this case we have two files named '0' and '1'.

Cards refer to this material in the flds field of the note with html formatting, as demonstrated below. Note that the special separator character \xf1 (hex f1) is shown below as '[\xf1]' due to formatting restrictions.

Card front content<img src="07Dads.JPG" />[\xf1]Card back content<img src="ponyoreview.jpg" />

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.