Search code examples
javaandroiddatabasesqlitesqliteopenhelper

Android SQLite database sharing across activities


Background info: new to Android, not to Java.

I am writing an app that sends its own messages separately from the default Android sms app. Since my app is not the default, I can't write to Android's provider.sms.outbox, which is absolutely fine. To work around this, I am creating a SQLite database to store my app's outgoing messages (in message objects). I am struggling to find a good, detailed explanation/tutorial on how to create a SQLite database that is available across all of my activities. I only have 2 activities; one that reads the database, and one that reads/writes the database.

I know that I need to create a subclass of the SQLiteOpenHelper, and I have:

public class dbHelper extends SQLiteOpenHelper {
//Database Info
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "outgoingMsgs.db";
public static final String TABLE_MESSAGES = "messages";
//Table Info
public static final String COLUMN_ID = "id";
public static final String COLUMN_SEND_TO_NAME = "send_to_name";
public static final String COLUMN_SEND_TO_NUM = "send_to_num";
public static final String COLUMN_BODY = "msg_body";

public dbHelper(Context context){
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

//table creation...
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_MESSAGES_TABLE = "CREATE TABLE " + TABLE_MESSAGES +
            "(" + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_SEND_TO_NAME + " TEXT," +
            COLUMN_SEND_TO_NUM + " TEXT," + COLUMN_BODY + " TEXT" + ")";
    db.execSQL(CREATE_MESSAGES_TABLE);
}
//database version upgrade... destroys old and recreates
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_MESSAGES);
    onCreate(db);
}

//adds single record
public void addRecord(myMessage msg){
    SQLiteDatabase dbase = this.getWritableDatabase();
    ContentValues vals = new ContentValues();
    //fill vals with appropriate content
    vals.put(COLUMN_SEND_TO_NAME, msg.get_name());
    vals.put(COLUMN_SEND_TO_NUM, msg.get_number());
    vals.put(COLUMN_BODY, msg.getBody());
    //insert
    dbase.insert(TABLE_MESSAGES, null, vals);
    dbase.close();
}

public int getRecordCount(){
    String countQuery = "SELECT * FROM " + TABLE_MESSAGES;
    SQLiteDatabase dbase = this.getReadableDatabase();
    Cursor cursor = dbase.rawQuery(countQuery, null);
    cursor.close();
    return cursor.getCount();
}

//deletes a single record
public void deleteRecord(myMessage msg){
    SQLiteDatabase dbase = this.getWritableDatabase();
    dbase.delete(TABLE_MESSAGES, COLUMN_ID + " = ?",
            new String[] { String.valueOf(msg.get_id()) });
    dbase.close();
}
}

Now, my problem is that I cannot figure out where (in which Activity or even in an Activity) or how I should create the instance of my dbHelper to make it available across my 2 activities. I know the singleton pattern is used frequently and the ContentProvider method is preferred. I don't feel that I'm ready to use ContentProviders with my very limited Android knowledge, so I would like to explore the singleton method. Can anyone please help me through this process?


Solution

  • how I should create the instance of my dbHelper to make it available across my 2 activities

    For what? You can just create a new instance for each Activity, since they use the very same database (outgoingMsgs.db).

    For example, if you call addRecord in Activity A and use the different instance of dbHelper in Activity B to call getRecordCount, the inserted data will be selected.