Connect to SQLite database



I am trying to connect to a SQLite database and since there is no officially supported driver; I decided to use the sqlite-jdbc driver and port the respective code into Mathematica. However, the DriverManager refuses to connect to the database, the error message is:

Java::excptn: A Java exception occurred: java.sql.SQLException: 
No suitable driver found for jdbc:sqlite:C:/sqlite/test.db
at java.sql.DriverManager.getConnection(
at java.sql.DriverManager.getConnection(

The driver & the code that I am trying to port is at: and the respective lines that I am having issue is:


Connection connection = null;
  // create a database connection
  connection = DriverManager.getConnection("jdbc:sqlite:sample.db");

I noticed the code uses a Class.forName() and I used the JLinkClassLoader to load the same class. Here are my efforts so far:

(*location of the driver jar*)

LoadJavaClass /@ 
 {"java.sql.Connection", "java.sql.DriverManager",  "java.sql.ResultSet", 
  "java.sql.Statement", "java.sql.SQLException","org.sqlite.JDBC"}


(*Errors out with the message above*)

I used DriverManager.getDrivers() and it shows me that only the sun.jdbc.odbc driver has been loaded; so the error message is justified.

I am at this point not sure how to proceed /debug the issue and will greatly appreciate any assistance.

If you have any suggestions, I will be very grateful.


Posted 2013-05-31T23:50:42.567

Reputation: 131

That's odd, on Mac OS X, Mathematica version 9, there's a sqlite3 library (in, and I can open and fetch information from it using DatabaseQueryDatabase`... I wonder why that's not available on Windows? – cormullion – 2013-06-01T07:31:10.280

@cormullion: what you describe is an undocumented (?) feature (direct access to sqlite databases) which also exists on Windows and is described e.g. here. It might well serve the purposes of the OP but isn't related to accessing them via ``DatabaseLink```, which is what the OP tried. I think that should also be possible with a decent setup...

– Albert Retey – 2013-06-01T09:44:32.010

Did you test that the Java code you're trying to port works on your system? – celtschk – 2013-06-01T09:59:52.650

I'm not quite sure but my suspection is that what you do is correct for making it available to Java, but that might not be enough to make it available to DatabaseLink. There is a description on how to make available third party JDBC drivers in the documentation of DatabaseLink. I had to do that once and remember that it was somewhat simpler than what you do, it certainly wasn't necessary to fiddle with all that low level Java stuff. It's not entirely clear from your question, but have you tried what is described in the documentation? – Albert Retey – 2013-06-01T10:16:24.370

@AlbertRetey yes, true, but I wasn't sure if the OP had rejected that option, so thought it worth mentioning. There's sometimes an overlap between "officially supported" "undocumented", and "works just fine" ...:) – cormullion – 2013-06-01T10:45:59.883

@cormullion: by no means I wanted to indicate that anything was wrong with suggesting Database`QueryDatabase. In fact I think it probably is the best and certainly the easiest thing to try for the OP and I think you should make that suggestion an answer specially because it also should work on Windows (I tried with 9.0.1 on Windows 7 with success). I just felt you and the OP might talk at cross purposes so I wanted to make clear that these are two completely different things. I think using DatabaseLink` seems to be justfied only for easier portability to other database engines.. – Albert Retey – 2013-06-01T20:59:02.457



As of Mathematica v10, SQLite seems officially supported (at least on OS X):

Select[JDBCDriverNames[], StringMatchQ[#, ___ ~~ "SQLite" ~~ ___] &]

(* {"SQLite", "SQLite(Memory)"} -> not in v9, new in v10! *)

Testing with the Chinook example database:

conn = OpenSQLConnection[JDBC["SQLite", "/Users/username/Downloads/Chinook_Sqlite.sqlite"]];

(* {SQLTable["Album", "TableType" -> "TABLE"], 
     SQLTable["Artist", "TableType" -> "TABLE"], 
     SQLTable["Customer", "TableType" -> "TABLE"], 
     SQLTable["Employee", "TableType" -> "TABLE"], 
     SQLTable["Genre", "TableType" -> "TABLE"], 
     SQLTable["Invoice", "TableType" -> "TABLE"], 
     SQLTable["InvoiceLine", "TableType" -> "TABLE"], 
     SQLTable["MediaType", "TableType" -> "TABLE"], 
     SQLTable["Playlist", "TableType" -> "TABLE"], 
     SQLTable["PlaylistTrack", "TableType" -> "TABLE"], 
     SQLTable["Track", "TableType" -> "TABLE"]} *)


Posted 2013-05-31T23:50:42.567

Reputation: 517

+1: very good finding, I just checked that it is also available on Windows. – Albert Retey – 2015-01-23T17:12:40.657


edit march 2017

in version 11.1 the Database` functions as described in the original answer following have been removed. As Jerome mentioned in his answer since version 10 there is a SQLite JDBC driver coming with DatabaseLink` which is of course the best (and officially supported) way to access SQLite databases in all versions newer than 9.

original answer

I'm not sure but don't think the problem is with your setup of the JDBC driver. There is reasonable documentation how you can make JDBC drivers available in the DatabaseLink documentation, which you can find here: JDBC Connections. It is basically as easy as putting the jar that you can download into the right directory (It might well be that what Stefan did would also have worked just as well). I did put mine into FileNameJoin[{$UserBaseDirectory, "Applications", "SQLite", "Java"}]. That was all that was necessary to make this succeed:

db = OpenSQLConnection[JDBC[
  "org.sqlite.JDBC",(* driver classname *)
  "jdbc:sqlite:C:\\path\\to\\database.sqlite" (*connection string*)

Just as Stefan, I could then also evaluate SQLTables[db] succesfully but any kind of select does then give the same error that he sees ("JDBC::error: ResultSet closed."). One could try to use some more low level approch and accessing the result set directly which can be done using DatabaseLink, but I wouldn't be very optimistic that you can get that working. There is certainly something going wrong here...

On the other hand, the use of the internal functions to access SQLite databases as cormullion has suggested in a comment for the same connection did work alright:

db = Database`OpenDatabase["C:\\path\\to\\database.sqlite"]

Database`QueryDatabase[db, "SELECT * FROM sqlite_master WHERE type=\"table\""]

Database`QueryDatabase[db, "SELECT * FROM webappsstore2"]


To me that looks like some incompatibility of the given JDBC driver for SQLite and what DatabaseLink tries to do with it. If you don't explicitly need to make access with a JDBC driver and DatabaseLink I'd suggest to just use the internal functionality, which I think is used for internal stuff and is there since version 7 so it might not go away anytime soon (but of course that could happen). There is even a recipe how you can compile another sqlite library and replace the one that ships with Mathematica here, in case that's of interest (I certainly wouldn't suggest to do that if not for very good reasons).

If you need this to work with a JDBC driver and DatabaseLink you might need to find another driver, either another vendor (there is a long list in the sqlite-wiki) or another version. Of course there are no guarantees that any of them will work, but if you have the time, you could try them (if you do, I'm sure there users of this site would appreciate if you come back and report). There are also listed some OBDC wrappers in that wiki, which is what you could try as a last resort, but using a bridge cascade like DatabaseLink -> JDBC -> ODBC -> SQLite doesn't sound like a straightforward and reliable solution and I'd not be surprise if that wouldn't work either.

Of course there are plenty of other possibilities to access the database nevertheless when bypassing DatabaseLink and e.g. access the JDBC Java object directly or use LibraryLink for direct access to the SQLite C-library -- but then one probably is better off to just use the internal functionality...

Edit: I played with this some more and found that even a SQLResultSetOpen will give the same error. I also tried the preview for the newest version of that driver with the same result. If one doesn't set Mode -> "ForwardOnly" for SQLResultSetOpen one will see another error message indicating that SQLite only supports forward only. So it seems that either that driver or SQLite in general only support a part of the full JDBC functionality and that DatabaseLink tries to use some of the nonexisting functionality. I'm not enough of an expert about DatabaseLink, JDBC and SQLite to know whether that is a restriction which could in principle be removed or not. Maybe it isn't by accident that there is internal functionality for the access to SQLite but no JDBC driver coming with the standard DatabaseLink package...

Albert Retey

Posted 2013-05-31T23:50:42.567

Reputation: 22 455

I'll also note (for those that might run into the same problem) that the connections need absolute paths, not relative paths. For instance, on OS X/Linux, a path like ~/foo/bar/qux.db won't work, while /Users/abc/foo/bar/qux.db will. – rm -rf – 2014-04-08T16:05:56.387

Useful answer - I wonder whether the OP will end up going the built-in SQLite route... – cormullion – 2013-06-02T08:45:41.353

@cormullion: thanks, it is by large parts yours as well :-) – Albert Retey – 2013-06-02T09:18:05.187


What I achieved so far is this:

  1. Set the correct ClassPath

  2. Load DatabaseLink

  3. establish a connection with JDBC

    conn = DatabaseLink`OpenSQLConnection[
  4. proof the connection was established successfully

    (* -> SQLConnection[1,Open,TransactionIsolationLevel->Serializable] *)
  5. List the tables (this database has a table memos. Created as described in the man-page for sqlite3)

    (* -> {SQLTable["memos", "TableType" -> "TABLE"]} *)
  6. Execute a Select

    SQLSelect[conn, "memos"]

But, at this point I immediately get a JDBC::error : ResultSet closed.

Maybe someone has more success at this specific step...


Posted 2013-05-31T23:50:42.567

Reputation: 5 207

I've the same error ! Did someone find the problem ? – msalese – 2014-05-12T12:13:05.553

I am distressed by how 'close Mathematica and reopen it' sometimes turns out to be a solution to errors. – Chris F Carroll – 2020-11-14T00:04:49.357