Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/brodycj/android-sqlite-native-ndk-connector


https://github.com/brodycj/android-sqlite-native-ndk-connector

Last synced: about 2 months ago
JSON representation

Awesome Lists containing this project

README

        

# Android SQLite native NDK connector

Java classes with abstract interface layers to provide a simple, easy-to-use Java interface to the NDK library (may be adapted for other Java environments).

With a simple test Android app included.

Based on: [`liteglue / Android-sqlite-connector`](https://github.com/liteglue/Android-sqlite-connector)

by Christopher J. Brody aka Chris Brody mailto:

with some complex API response type enhancements by Luis Silva of OutSystems (luis dot silva at outsystems dot com)

License: UNLICENSE (public domain).

## Dependencies

### Included:

TBD

### Not included

Android NDK library JAR build, from this for example: [`brodybits / android-sqlite-ndk-native-driver`](https://github.com/brodybits/android-sqlite-ndk-native-driver)

## Alternative native library builds

TBD

## Installation

Include the following in your `libs` directory:

- NDK JAR library
- `sqlite-native-ndk-connector.jar`, which is built by simply issuing the `make` command in this project

## Testing

There is a simple test project in the `Android-SQLiteConnectorTest` subdirectory. To test:

- Use the `make` command to build the JAR
- Build the NDK JAR
- `cd Android-SQLiteConnectorTest`
- Install the JAR files into the `libs` subdirectory of `Android-SQLiteConnectorTest`
- Use a tool such as Android Studio to build and run (import is needed for newer versions of Android Studio)

## Sample API Usage

**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.

### First step

Import `io.liteglue` package:

```Java
import io.liteglue.*;
```

Get a SQLiteConnector (factory) instance:

```Java
SQLiteConnector myconnector = new SQLiteConnector();
```

### Open a database

```Java
File dbfile = new File(getFilesDir(), "my.db");

SQLiteConnection mydbc = myconnector.newSQLiteConnection(dbfile.getAbsolutePath(),
SQLiteOpenFlags.READWRITE | SQLiteOpenFlags.CREATE);
```

### Prepare and run a simple statement (with no parameters)

```Java
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.

### Prepare and run a statement with parameter values

```Java
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();
```

### SELECT data and get row result(s)

```Java
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