Please update to the latest Anki version

So I’m trying to read an .akpg in python. This is my code

import sqlite3
import os
import tempfile
import zipfile

def read_collection_anki2(apkg_path):
    """
    Reads data directly from the collection.anki2 file within an .apkg.

    Args:
        apkg_path (str): The path to the .apkg file.
    """
    try:
        with tempfile.TemporaryDirectory() as tmpdir:
            with zipfile.ZipFile(apkg_path, 'r') as apk_file:
                apk_file.extract('collection.anki2', tmpdir)
                db_path = os.path.join(tmpdir, 'collection.anki2')

            conn = sqlite3.connect(db_path)
            cursor = conn.cursor()

            # Now you can execute SQL queries on the 'cursor' object
            # to read data from the tables in the database.

            # Example: Get a list of all tables
            cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
            tables = [row[0] for row in cursor.fetchall()]
            print("Tables in the database:", tables)

            # Example: Get the number of notes
            cursor.execute("SELECT COUNT(*) FROM notes")
            note_count = cursor.fetchone()[0]
            print(f"Number of notes: {note_count}")

            # Example: Fetch the first 10 notes (id, guid, mid, flds)
            cursor.execute("SELECT id, guid, mid, flds FROM notes LIMIT 10")
            first_notes = cursor.fetchall()
            for note in first_notes:
                print(f"Note ID: {note[0]}, GUID: {note[1]}, Model ID: {note[2]}, Fields: {note[3]}")

            # Remember to close the connection
            conn.close()

    except zipfile.BadZipFile:
        print(f"Error: Invalid .apkg file: {apkg_path}")
    except sqlite3.Error as e:
        print(f"Error accessing the Anki database: {e}")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

if __name__ == "__main__":
    apkg_file_path = 'anki_spanish_deck.apkg'  # Replace with the actual path
    read_collection_anki2(apkg_file_path)

and the akpg im reading gives me this output:

$ python read_anki_file.py 
Tables in the database: ['col', 'notes', 'cards', 'revlog', 'sqlite_stat1', 'sqlite_stat4', 'graves']
Number of notes: 1
Note ID: 1746629856928, GUID: D4/[1ERfG~, Model ID: 1746629856923, Fields: Please update to the latest Anki version, then import the .colpkg/.apkg file again.

I updated my anki version so I dont know why there’s still an issue.

1 Like

Hi everyone, I figured it out! It turns out a lot of the code references online use collection.anki2, but apparently now it’s collection.anki21 for the newer version. I feel like this may be a typo. But everything works now! I feel like collection.anki2 needs to have a deprecation message.

1 Like

Also more of an update, I dug into the code repo and found the reference. Turns out collection.anki21 is an experimental v2. But doesn’t seem experimental since it’s not in beta. Can we please remove .anki2 if it’s no longer needed.

1 Like