Giter Club home page Giter Club logo

android-sqlite-connector's Introduction

Android sqlite connector

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: [email protected]

License: UNLICENSE (public domain).

Dependencies

Included:

Not included

TBD (TEMPORARILY BROKEN) Alternative native library builds

WARNING: 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

Installation

Include the following in your libs directory:

Testing

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

  • Build sqlite-connector.jar in this project (using the make command)
  • Build the sqlite-native-driver native libs in @liteglue / Android-sqlite-native-driver
  • cd Android-SQLiteConnectorTest
  • Install 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 above
  • You may have to update your project using a command like: android update project -p .
  • install it on your emulator or device: ant debug install
  • run it on your emulator or device

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:

import io.liteglue.*;

Get a SQLiteConnector (factory) instance:

SQLiteConnector myconnector = new SQLiteConnector();

Open a database

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");

Prepare and run a simple statement (with no parameters)

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

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)

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

Close the database connection

mydbc.dispose();

Internals

  • Multiple layers with abstract interfaces to make it easier to replace the lower-level SQLiteGlue native database interface

android-sqlite-connector's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.