Connection to Anki Database in Java

Dear all,

I’m relatively new to Anki, so I hope it ist ok for you if I ask this question here.

I did a lot of google-research, but I haven’t found an answer to one fundamental question so far.

Anki is a great software, I really like it … for my purposes, I created large decks to practise English and French.

I’d like to aminister these decks programmtically, i. e. to add new entries, modify/update existing entries and so on … and I’d like to do it using Java, my Python knowledge is only fundamental.

So far, I’ve found the Ankidroid source here

AnkiDroid at Github, here: DB.java

Here

at javatips.net

I’ve found a version of AnkiDb.java which seems to be a liitle bit older … but it must be something like this:

mDatabase = new SQLiteDataSource(config);
mDatabase.setUrl("jdbc:sqlite:" + ankiFilename);

But I can’t figure out how to indicate the correct database adress … and how to provide the ankinet-password?

I’m a Java-programmer, and I’m going to do this on a Linux system (SuSe Leap 15.2) … the operating system has SQLite-Drivers, and I’m able to figure out the necessary SQL-statements on my own. I have a SWING-Dialog which can mask the password … but I can’t figure out how to establish the database connection.

So I would really appreciate your help if you’d give me an answer … maybe a link, it is possible that I’ve overlooked which has been solved in the forum here … or a piece of Python code which I can “translate” into Java code …

Thanks in advance and kind regards, Frank

I don’t know much about how AnkiDroid accesses the database, but I think you can use the standard JDBC API along with a SQLite JDBC driver like this one.

Assuming the Anki database file is collection.anki2 (as it’s stored in your profile folder), you can start by making a connection to it using something like this:

Connection conn = DriverManager.getConnection("jdbc:sqlite:collection.anki2");

For a tutorial on the JDBC API, see: Getting Started (The Java™ Tutorials > JDBC Database Access > JDBC Basics)

Anyway, I don’t recommend making modifications to the database directly unless you know what you’re doing.
You can use the Python API provided by Anki to do that. It’s not hard to use really.
See Writing Anki Add-ons for starters, or take a look at the AnkiConnect add-on if you want to modify your collection using whatever language you want.

2 Likes

This was helpful … thank you very much!

Using Eclipse, it was easy to prepare the sqlite via pom.xml as dependency …

Connection conn = DriverManager.getConnection(mPathToAnkiDB);
Statement stmt = conn.createStatement();
System.out.println("Success");
ResultSet res = stmt.executeQuery("SELECT count() FROM cards");
while ( res.next () )
{
    System.out.println( res.getInt(1) );
}
conn.close();

Indeed, on the console, it says:

Success
15933

I guess this number is correct :grinning:

In the meantime, I’ve done some research in the Ankidroid sources … this will help to figure out all the necessary SQL statements.

By the way, a nice description can be found here at www.juliensobczak.com

Anyway, I don’t recommend making modifications to the database directly unless you know what you’re doing.

Sure … that’s why I will be careful, otherwise I run the risk to destroy all my work …

I developed some Java code to handle my cards … at the moment, I create text files which can be imported into Anki, but this is too cumbersome :grinning:

Thanks again, and best wishes, Frank

1 Like