Java and SQLite [closed]
Asked Answered
C

9

336

I'm attracted to the neatness that a single file database provides. What driver/connector library is out there to connect and use SQLite with Java.

I've discovered a wrapper library, http://www.ch-werner.de/javasqlite, but are there other more prominent projects available?

Canaster answered 3/9, 2008 at 6:10 Comment(0)
D
196

The wiki lists some more wrappers:

Disdain answered 3/9, 2008 at 6:17 Comment(9)
My addition to this list is sqlite4java - code.google.com/p/sqlite4java - it's a wrapper (no JDBC); precompiled for Windows, Mac, Linux. It's simple to use and it enforces some contracts to help the developer avoid misusing SQLite.Spanner
sqlite4java looks interesting, but they also have a great comparison of the various wrappers out there: code.google.com/p/sqlite4java/wiki/ComparisonToOtherWrappersCanaster
@kdt the problem I found with the zentus driver is that it doesn't seem to support BLOB's at allColorless
@Colorless xerial.org/trac/Xerial/wiki/SQLiteJDBC is a fork of zentus's driver and supports BLOB (there's a short tut on their site).Sisera
zentus.com/sqlitejdbc -> 404Morman
@Martijn: are you sure? Because there's an open issue about it not supporting blobs (1 1/2 year later...).Legit
@Legit not anymore, haven't looked at it for ages. I know (quite sure) it bit me in the behind once.Colorless
archive.org link web.archive.org/web/20080309203650/http://www.sqlite.org/…Julian
The Xerial driver definitely supports blobs (though not the java.sql.Blob type). I've been using it to manage blobs for years. However, just… don't concatenate blobs; that's horribly slow.Joist
P
250

I found your question while searching for information with SQLite and Java. Just thought I'd add my answer which I also posted on my blog.

I have been coding in Java for a while now. I have also known about SQLite but never used it… Well I have used it through other applications but never in an app that I coded. So I needed it for a project this week and it's so simple use!

I found a Java JDBC driver for SQLite. Just add the JAR file to your classpath and import java.sql.*

His test app will create a database file, send some SQL commands to create a table, store some data in the table, and read it back and display on console. It will create the test.db file in the root directory of the project. You can run this example with java -cp .:sqlitejdbc-v056.jar Test.

package com.rungeek.sqlite;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class Test {
    public static void main(String[] args) throws Exception {
        Class.forName("org.sqlite.JDBC");
        Connection conn = DriverManager.getConnection("jdbc:sqlite:test.db");
        Statement stat = conn.createStatement();
        stat.executeUpdate("drop table if exists people;");
        stat.executeUpdate("create table people (name, occupation);");
        PreparedStatement prep = conn.prepareStatement(
            "insert into people values (?, ?);");

        prep.setString(1, "Gandhi");
        prep.setString(2, "politics");
        prep.addBatch();
        prep.setString(1, "Turing");
        prep.setString(2, "computers");
        prep.addBatch();
        prep.setString(1, "Wittgenstein");
        prep.setString(2, "smartypants");
        prep.addBatch();

        conn.setAutoCommit(false);
        prep.executeBatch();
        conn.setAutoCommit(true);

        ResultSet rs = stat.executeQuery("select * from people;");
        while (rs.next()) {
            System.out.println("name = " + rs.getString("name"));
            System.out.println("job = " + rs.getString("occupation"));
        }
        rs.close();
        conn.close();
    }
  }
Pyszka answered 27/2, 2009 at 0:49 Comment(6)
This is why I think Joel was talking bull (Google tech talk:youtube.com/watch?v=NWHfY_lvKIQ) when saying that stackoverflow.com's design is better because of all the "old" posts hanging around on the internet. It's just a rehash.Comeon
Also notice that Crawshaw project seems on hiatus, but was forked and updated here: xerial.org/trac/Xerial/wiki/SQLiteJDBCFixture
zentus.com seems broken, found mirror here; priede.bf.lu.lv/pub/DatuBazes/SQLite/SqliteJDBC/about.htmPray
Thanks @DanielMagnusson, you're a life saver. While on the topic, anyone still looking for the sqliteJDBC driver can go priede.bf.lu.lv/pub/DatuBazes/SQLite/SqliteJDBC/… since the link the about.htm links to is incorrect.Magma
maven dependency: mvnrepository.com/artifact/org.xerial/sqlite-jdbcSamovar
We need sqlite-jdbc on other computer? where application is being used?Mauricemauricio
D
196

The wiki lists some more wrappers:

Disdain answered 3/9, 2008 at 6:17 Comment(9)
My addition to this list is sqlite4java - code.google.com/p/sqlite4java - it's a wrapper (no JDBC); precompiled for Windows, Mac, Linux. It's simple to use and it enforces some contracts to help the developer avoid misusing SQLite.Spanner
sqlite4java looks interesting, but they also have a great comparison of the various wrappers out there: code.google.com/p/sqlite4java/wiki/ComparisonToOtherWrappersCanaster
@kdt the problem I found with the zentus driver is that it doesn't seem to support BLOB's at allColorless
@Colorless xerial.org/trac/Xerial/wiki/SQLiteJDBC is a fork of zentus's driver and supports BLOB (there's a short tut on their site).Sisera
zentus.com/sqlitejdbc -> 404Morman
@Martijn: are you sure? Because there's an open issue about it not supporting blobs (1 1/2 year later...).Legit
@Legit not anymore, haven't looked at it for ages. I know (quite sure) it bit me in the behind once.Colorless
archive.org link web.archive.org/web/20080309203650/http://www.sqlite.org/…Julian
The Xerial driver definitely supports blobs (though not the java.sql.Blob type). I've been using it to manage blobs for years. However, just… don't concatenate blobs; that's horribly slow.Joist
R
31

I understand you asked specifically about SQLite, but maybe HSQL database would be a better fit with Java. It is written in Java itself, runs in the JVM, supports in-memory tables etc. and all that features make it quite usable for prototyping and unit-testing.

Retinoscopy answered 27/2, 2009 at 12:13 Comment(1)
Yes HSQL is a very good choice and I've used it quite extensively in a couple of client apps for good effect. In this instance however, I did indeed want to use SQLite.Canaster
P
19

David Crawshaw project(sqlitejdbc-v056.jar) seems out of date and last update was Jun 20, 2009, source here

I would recomend Xerials fork of Crawshaw sqlite wrapper. I replaced sqlitejdbc-v056.jar with Xerials sqlite-jdbc-3.7.2.jar file without any problem.

Uses same syntax as in Bernie's answer and is much faster and with latest sqlite library.

What is different from Zentus's SQLite JDBC?

The original Zentus's SQLite JDBC driver http://www.zentus.com/sqlitejdbc/ itself is an excellent utility for using SQLite databases from Java language, and our SQLiteJDBC library also relies on its implementation. However, its pure-java version, which totally translates c/c++ codes of SQLite into Java, is significantly slower compared to its native version, which uses SQLite binaries compiled for each OS (win, mac, linux).

To use the native version of sqlite-jdbc, user had to set a path to the native codes (dll, jnilib, so files, which are JNDI C programs) by using command-line arguments, e.g., -Djava.library.path=(path to the dll, jnilib, etc.), or -Dorg.sqlite.lib.path, etc. This process was error-prone and bothersome to tell every user to set these variables. Our SQLiteJDBC library completely does away these inconveniences.

Another difference is that we are keeping this SQLiteJDBC libray up-to-date to the newest version of SQLite engine, because we are one of the hottest users of this library. For example, SQLite JDBC is a core component of UTGB (University of Tokyo Genome Browser) Toolkit, which is our utility to create personalized genome browsers.

EDIT : As usual when you update something, there will be problems in some obscure place in your code(happened to me). Test test test =)

Pray answered 5/9, 2012 at 9:24 Comment(1)
The sqlite-jdbc project is great, but be aware that it is Apache licensed. Therefore you must give attribution if you use it, whether your code is FOSS or proprietary.Nildanile
I
16

There is a new project SQLJet that is a pure Java implementation of SQLite. It doesn't support all of the SQLite features yet, but may be a very good option for some of the Java projects that work with SQLite databases.

Incognizant answered 10/8, 2009 at 23:48 Comment(3)
It looks promising but it appears that it doesn't offer an SQL query ability yet, kind of a deal breaker for me.Canaster
Still does not support SQL queries, rather, a lower level API.Nellenelli
Still very useful for applications where you only need to generate SQLite files, i.e., don't need SQL to query the data.Meso
P
3

When you compile and run the code, you should set the classpath options value. Just like the following:

javac -classpath .;sqlitejdbc-v056.jar Text.java

java -classpath .;sqlitejdbc-v056.jar Text

Please pay attention to "." and the sparate ";"(win, the linux is ":")

Photofinishing answered 24/2, 2012 at 7:10 Comment(0)
B
3

sqlitejdbc code can be downloaded using git from https://github.com/crawshaw/sqlitejdbc.

# git clone https://github.com/crawshaw/sqlitejdbc.git sqlitejdbc
...
# cd sqlitejdbc
# make

Note: Makefile requires curl binary to download sqlite libraries/deps.

Bratwurst answered 7/11, 2012 at 16:10 Comment(0)
U
2

The example code leads to a memory leak in Tomcat (after undeploying the webapp, the classloader still remains in memory) which will cause an outofmemory eventually. The way to solve it is to use the sqlite-jdbc-3.7.8.jar; it's a snapshot, so it doesn't appear for maven yet.

Uroscopy answered 10/1, 2012 at 0:59 Comment(0)
M
0

Typo: java -cp .:sqlitejdbc-v056.jar Test

should be: java -cp .:sqlitejdbc-v056.jar; Test

notice the semicolon after ".jar" i hope that helps people, could cause a lot of hassle

Mudskipper answered 28/1, 2012 at 15:35 Comment(1)
Care to elaborate? On *nix the ; will seperate the java from the Test command (and therefore create a error). On Windows the : wont work as a cp seperator. So all in all .:xxx.jar; does not make sense. And you also need to specify the package name for the Test class.Idolatry

© 2022 - 2024 — McMap. All rights reserved.