SQLiteJDBC

<- | SQLiteJDBC | Changelog | Usage | Speed | Custom Functions | Javadoc

Usage

Download the binary for the platform you are developing on. Open the tarball and copy the two files into your application directory:

    sqlitejdbc.jar
    sqlitejdbc.dll (or libsqlitejdbc.so or libsqlitejdbc.jnilib)

Reference the driver in your code:

    Class.forName("org.sqlite.JDBC");
    Connection conn = DriverManager.getConnection("jdbc:sqlite:filename");
    // ... use the database ...
    conn.close();

And call your program with the driver's JAR file in the classpath and the C library in the librarypath. E.g.

    java -cp sqlitejdbc.jar -Djava.library.path=. yourclass

That's it.

Dates and Time

SQLiteJDBC implements ResultSet.getDate()/getTime() and PreparedStatement.setDate()/setTime() in the only efficient way the JDBC spec allows for, which is storing the time as a 64-bit long of milliseconds since UTC. This is the standard unix timestamp.

This does not conflict with the SQLite standard format as long as care is taken to transform the unix timestamp into what SQLite functions use, either at the point where the date is stored or when using it elsewhere in SQLite. For example:

    prep = conn.prepareStatement("insert into test values (?);");
    prep.setDate(1, new Date(1000));
    prep.executeUpdate();

    rs = stat.executeQuery("select * from test;");
    rs.getString(1) == "1000";
In the above example the date is being stored as a unix timestamp. To store the date in the standard SQLite format, transform it with the datetime() and strftime() functions:
    prep = conn.prepareStatement(
        "insert into test values datetime(? / 1000, 'unixepoch');");
    prep.setDate(1, new Date(1092941466000));
    prep.executeUpdate();

    rs = stat.executeQuery("select * from test;");
    assert(rs.getString(1).equals("2004-08-10 18:51:06"));

    rs = stat.executeQuery("select strftime('%s', col1) * 1000 from test;");
    assert(rs.getDate(1).equals(new Date(987654321)));

Compiling

Download the source tarball, extract, set your $JAVA_HOME env variable and type:

    $ make

See the README for more details.

2006-09-26