Android fully supports SQLite DBs. All the DBs you create inside your app will be accessible from all the classes of your apps (NOT outside your app!).
First of all, you should create a Java class that extends SQLiteOpenHelper. Here, you have to override the onCreate() and onUpdate() methods. As one can suppose, the first is called when the DB is created, while the latter is called when the DB is modified.
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.Context;
import android.database.Cursor;
import android.content.ContentValues;
/**
* Class DatabaseHelper.
*/
public class DatabaseHelper extends SQLiteOpenHelper {
//Database parameters:
private static final String DATABASE_NAME = "dbname"; // the name of the DB!
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_TABLE_NAME = "tripmileagetable"; // the name of the table!
//Table attributes:
private static final String DATABASE_TABLE_ATTR_ID = "id"; // attr1
private static final String DATABASE_TABLE_ATTR_ODOMETER = "odometer"; // attr2
private static final String DATABASE_TABLE_ATTR_DATE = "date"; // attr3
/**
* Class constructor.
*
* @param context the context.
*/
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String create = "CREATE TABLE " + DATABASE_TABLE_NAME + " (" +
DATABASE_TABLE_ATTR_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
DATABASE_TABLE_ATTR_ODOMETER + " INTEGER, " +
DATABASE_TABLE_ATTR_DATE + " TEXT);";
db.execSQL( create );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_NAME);
onCreate(db);
}
}
Now, inside the new Java class, you can create custom methods to query your DB.
To write the DB you should use getWritableDatabase(), while to read it you should use getReadableDatabase(). Both of them return an SQLiteDatabase object, and eventually throw an SQLiteException.
In particular, to query your DB, there are two SQLiteDatabase methods available: rawQuery and query (both return a Cursor object).
/**
* Get the sum of the odometer of a particular month and year.
*
* @param year the year.
* @param month the month.
* @return the sum of the odometer of the year and the month.
*/
public int sumOdometer(Integer year, Integer month) {
//Date composition:
String date = year.toString() + "-" + month.toString() + "%";
//SQL query:
String query = "SELECT SUM(" + DATABASE_TABLE_ATTR_ODOMETER + ") AS " + DATABASE_TABLE_ATTR_ODOMETER +
" FROM " + DATABASE_TABLE_NAME +
" WHERE " + DATABASE_TABLE_ATTR_DATE + "LIKE ?";
//Execute the SQL query:
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery(query, new String [] {date});
int sum = 0;
if( cursor.moveToFirst() ) { // moves the cursor to the first row in the result set...
sum = cursor.getInt( cursor.getColumnIndex(DATABASE_TABLE_ATTR_ODOMETER) );
}
//Close the Cursor:
cursor.close();
return sum;
}
Note that SQLite automatically puts single quotes (') around the arguments (?).
You can find a good tutorial here: http://www.codeproject.com/Articles/119293/Using-SQLite-Database-with-Android
GROUP BYto sum the data?