From Anonymous To User Database

Fabio Lee
FabioHub
Published in
2 min readMar 28, 2017

--

For a standard mobile app that required database as a storage, the sample code normally look like this:

public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "anonymous.db";
private static final int DATABASE_VERSION = 1;

private static DatabaseHelper instance;

public static DatabaseHelper getInstance(Context context) {
if (instance == null) {
instance = newInstance(context.getApplicationContext());
}
return instance;
}

private static DatabaseHelper newInstance(Context context) {
return new DatabaseHelper(context, databaseFileName, null, DATABASE_VERSION);
}

private DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override public void onCreate(SQLiteDatabase db) {
}

@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}

A standard SQLiteOpenHelper & an utility method to trigger an INSERT statement.

public void insert(Context context, String table, ContentValues values) {
SQLiteDatabase db = DatabaseHelper.getInstance(context).getWritableDatabase();
db.insert(table, null, values);
}

However, when the above standard mobile app with only one “anonymous.db” database start to implement account login feature, a quick question suddenly pop out, how should we handle the logged in users data?

Lets say “User A” logged in, bookmark “Item 1” in the mobile app which is been stored in the database. Then “User A” logout, lets “User B” to login with the same device to bookmark “Item 2”. Lastly lets “User B” logout, and “User A” login again. Will “User A” see “Item 1” as bookmarked?

Of course this can be achieved easily with the help of storing the users bookmark in the server, everytime when “User A” request for bookmarked items, can query from server database.

Fortunately, there is another way, by turning one “anonymous.db” database to multiple users database on the mobile app.

public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;

private static DatabaseHelper instance;

public static void clearInstance() {
instance = null;
}


public static DatabaseHelper getInstance(Context context) {
if (instance == null) {
instance = newInstance(context.getApplicationContext());
}
return instance;
}

private static DatabaseHelper newInstance(Context context) {
String accountName = "fabiolee"; // Get the account name from AccountAuthenticator or SharedPreferences
String databaseFileName;
if (accountName == null) {
databaseFileName = "anonymous.db"; // Anonymous Database
} else {
databaseFileName = accountName + ".db"; // User Database
}

return new DatabaseHelper(context, databaseFileName, null, DATABASE_VERSION);
}

private DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override public void onCreate(SQLiteDatabase db) {
}

@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}

By setting the database name from the account name, every logged in users will have their own unique database, no need to query from server database anymore.

Lastly, do remember to trigger DatabaseHelper.clearInstance(); after login or logout to switch database.

--

--