Internals

These function implement the more direct interactions with the Anki database and provide basic functionality that is then used to implement the functionality in Collection, ankipandas.ankidf.AnkiDataFrame etc.

Warning

Please only use these function if you know what you are doing, as they come with less consistency checks as the functionality implemented in Collection and ankipandas.ankidf.AnkiDataFrame. Also note that the functions here are considered to be internal, i.e. might change without prior notice.

ankipandas.raw.load_db(path: Union[str, pathlib.PurePath]) sqlite3.Connection[source]

Load database from path.

Parameters

path – String or pathlib.PurePath.

Returns

sqlite3.Connection

ankipandas.raw.close_db(db: sqlite3.Connection) None[source]

Close the database.

Parameters

db – Database (sqlite3.Connection)

Returns

None

ankipandas.raw.get_table(db: sqlite3.Connection, table: str) pandas.core.frame.DataFrame[source]

Get raw table from the Anki database.

Parameters
Returns

pandas.DataFrame

ankipandas.raw.get_empty_table(table: str) pandas.core.frame.DataFrame[source]

Get empty table

Parameters

tablecards, notes or revs

Returns

class: pandas.DataFrame

ankipandas.raw.read_info(db: sqlite3.Connection, table_name: str) dict[source]

Get a table from the database and return nested dictionary mapping of it.

Parameters
  • db

  • table_name

Returns:

ankipandas.raw.get_info(db: sqlite3.Connection) dict[source]

Get all other information from the database, e.g. information about models, decks etc.

Parameters

db – Database (sqlite3.Connection)

Returns

Nested dictionary.

ankipandas.raw.get_db_version(db: sqlite3.Connection) int[source]

Get “version” of database structure

Parameters

db

Returns: 0: all info (also for decks and models) was in the ‘col’ table

1: separate ‘notetypes’ and ‘decks’ tables

ankipandas.raw.set_table(db: sqlite3.Connection, df: pandas.core.frame.DataFrame, table: str, mode: str, id_column='id') None[source]

Write table back to database.

Parameters
  • db – Database (sqlite3.Connection)

  • df – The pandas.DataFrame to write

  • table – Table to write to: ‘notes’, ‘cards’, ‘revs’

  • mode – ‘update’: Update all existing entries, ‘append’: Only append new entries, but do not modify, ‘replace’: Append, modify and delete

  • id_column – Column with ID

Returns

None

class ankipandas.raw.NumpyJSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

Bases: json.encoder.JSONEncoder

JSON Encoder that support numpy datatypes by converting them to built in datatypes.

default(obj)[source]
ankipandas.raw.set_info(db: sqlite3.Connection, info: dict) None[source]

Write back extra info to database

Parameters
Returns

None

ankipandas.raw.get_ids(db: sqlite3.Connection, table: str) List[int][source]

Get list of IDs, e.g. note IDs etc.

Parameters
Returns

Nested dictionary

ankipandas.raw.get_deck_info(db: sqlite3.Connection) dict[source]

Get information about decks.

Parameters

db – Database (sqlite3.Connection)

Returns

Nested dictionary

ankipandas.raw.get_did2deck(db: sqlite3.Connection) Dict[int, str][source]

Mapping of deck IDs (did) to deck names.

Parameters

db – Database (sqlite3.Connection)

Returns

Dictionary mapping

ankipandas.raw.get_deck2did(db: sqlite3.Connection) Dict[str, int][source]

Mapping of deck names to deck IDs

Parameters

db – Database (sqlite3.Connection)

Returns

Dictionary mapping of deck id to deck name

ankipandas.raw.get_model_info(db: sqlite3.Connection) dict[source]

Get information about models.

Parameters

db – Database (sqlite3.Connection)

Returns

Nested dictionary

ankipandas.raw.get_mid2model(db: sqlite3.Connection) Dict[int, str][source]

Mapping of model IDs (mid) to model names.

Parameters

db – Database (sqlite3.Connection)

Returns

Dictionary mapping

ankipandas.raw.get_model2mid(db: sqlite3.Connection) Dict[str, int][source]

Mapping of model name to model ID (mid)

Parameters

db – Database (sqlite3.Connection)

Returns

Dictionary mapping

ankipandas.raw.get_mid2sortfield(db: sqlite3.Connection) Dict[int, int][source]

Mapping of model ID to index of sort field.

ankipandas.raw.get_mid2fields(db: sqlite3.Connection) Dict[int, List[str]][source]

Get mapping of model ID to field names.

Parameters

db – Database (sqlite3.Connection)

Returns

Dictionary mapping of model ID (mid) to list of field names.

ankipandas.raw.get_mid2templateords(db: sqlite3.Connection) Dict[int, List[int]][source]

Get mapping of model ID to available templates ids

Parameters

db

Returns:

ankipandas.raw.get_cid2nid(db: sqlite3.Connection) Dict[int, int][source]

Mapping card ID to note ID.

Parameters

db – Database (sqlite3.Connection)

Returns

Dictionary

ankipandas.raw.get_cid2did(db: sqlite3.Connection) Dict[int, int][source]

Mapping card ID to deck ID.

Parameters

db – Database (sqlite3.Connection)

Returns

Dictionary

ankipandas.raw.get_nid2mid(db: sqlite3.Connection) Dict[int, int][source]

Mapping note ID to model ID.

Parameters

db – Database (sqlite3.Connection)

Returns

Dictionary