Java classes with abstract interface layers to provide a simple, easy-to-use Java interface to the andriod-sqlite-native-driver library (may be adapted for other Java environments).
With a simple test Android app included.
by Christopher J. Brody aka Chris Brody mailto: brodybits@litehelpers.net
License: UNLICENSE (public domain).
src/io/liteglue/SQLiteNative.java
- low-level Java class from @liteglue / Android-sqlite-native-driversqlite-native-driver-libs.zip
as built from @liteglue / Android-sqlite-native-driversqlcipher-native-driver.jar
with native Android libraries that provide SQLCipher, as built from @liteglue / Android-sqlcipher-native-driverWARNING: It is recommended to extract the native per-CPU Android library subdirectories from the JAR and put them in the libs
directory of your project. Some newer Android systems do not
Include the following in your libs
directory:
libs
subdirectory from sqlite-native-driver-libs.zip
, as built from @liteglue / Android-sqlite-native-driver sqlcipher-native-driver.jar
as built in @liteglue / Android-sqlcipher-native-driversqlite-connector.jar
, which is built by simply issuing the make
command in this projectThere is a simple test project in the Android-SQLiteConnectorTest
subdirectory. To test:
sqlite-connector.jar
in this project (using the make
command)sqlite-native-driver
native libs in @liteglue / Android-sqlite-native-drivercd Android-SQLiteConnectorTest
sqlite-connector.jar
and the contents of the libs
subdirectory from sqlite-native-driver-libs.zip
into the libs
subdirectory of Android-SQLiteConnectorTest
as described aboveandroid update project -p .
ant debug install
IMPORTANT: Most of the methods described here will throw java.sql.SQLException
if the sqlite library reports an error or if they detect a problem with the usage.
Import io.liteglue
package:
import io.liteglue.*;
Get a SQLiteConnector (factory) instance:
SQLiteConnector myconnector = new SQLiteConnector();
File dbfile = new File(getFilesDir(), "my.db");
SQLiteConnection mydbc = myconnector.newSQLiteConnection(dbfile.getAbsolutePath(),
SQLiteOpenFlags.READWRITE | SQLiteOpenFlags.CREATE);
OPTIONAL, for use with sqlcipher-native-driver.jar
only: to specify the password key:
mydbc.keyNativeString("your-password");
SQLiteStatement mystatement = mydbc.prepareStatement("CREATE TABLE IF NOT EXISTS mytable (text1 TEXT, num1 INTEGER, num2 INTEGER, real1 REAL)");
mystatement.step();
mystatement.dispose();
IMPORTANT: Whenever SQLiteConnection.prepareStatement()
successfully returns a SQLiteStatement
, it must be cleaned up using its dispose()
method.
SQLiteStatement mystatement = mydbc.prepareStatement("INSERT INTO mytable (text1, num1, num2, real1) VALUES (?,?,?,?)");
mystatement.bindTextNativeString(1, "test");
mystatement.bindInteger(2, 10100);
mystatement.bindLong(3, 0x1230000abcdL);
mystatement.bindDouble(4, 123456.789);
mystatement.step();
mystatement.dispose();
SQLiteStatement mystatement = mydbc.prepareStatement("SELECT * FROM mytable;");
boolean keep_going = mystatement.step();
while (keep_going) {
int colcount = colcount = mystatement.getColumnCount();
android.util.Log.e("MySQLiteApp", "Row with " + colcount + " columns");
for (int i=0; i<colcount; ++i) {
int coltype = mystatement.getColumnType(i);
switch(coltype) {
case SQLColumnType.INTEGER:
android.util.Log.e("MySQLiteApp",
"Col " + i + " type: INTEGER (long) value: 0x" +
java.lang.Long.toHexString(mystatement.getColumnLong(i)));
break;
case SQLColumnType.REAL:
android.util.Log.e("MySQLiteApp",
"Col " + i + " type: REAL value: " + mystatement.getColumnDouble(i));
break;
case SQLColumnType.NULL:
android.util.Log.e("MySQLiteApp", "Col " + i + " type: NULL (no value)");
break;
default:
android.util.Log.e("MySQLiteApp",
"Col " + i + " type: " + ((coltype == SQLColumnType.BLOB) ? "BLOB" : "TEXT") +
" value: " + mystatement.getColumnTextNativeString(i));
break;
}
}
keep_going = mystatement.step();
}
mystatement.dispose();
mydbc.dispose();