The SQLite library itself has only a C API; to make it accessible from Java, the Android framework wraps this with the android.database.sqlite package. The most important classes are SQLiteDatabase and SQLiteOpenHelper.
The android.database package contains the database-related parts of the framework that are not SQLite specific.
API Level | Platform Version | Name | Release Date |
---|---|---|---|
1 | 1.0 | Base | 2008-09-23 |
5 | 2.0 | Eclair | 2009-10-26 |
8 | 2.2 | Froyo | 2010-05-20 |
11 | 3.0 | Honeycomb | 2011-02-22 |
16 | 4.1 | Jelly Bean | 2012-07-09 |
19 | 4.4 | Kitkat | 2013-10-31 |
23 | 6.0 | Marshmallow | 2015-10-05 |
To include a database in your app, you typically derive a class from SQLiteOpenHelper:
public class HelloDBHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final int DATABASE_NAME = "hello";
HelloDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE ...");
...
}
}
This helper class is responsible for opening (and creating/updating, if needed) the database. Use it to get an SQLiteDatabase object to access the data:
SQLiteDatabase db = helper.getReadableDatabase();
Cursor c = db.query(...);
while (c.moveToNext()) {
String name = c.getString(0);
...
}
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("column", value);
...
db.insertOrThrow("table", null, cv);