views:

73

answers:

1

Hi everyone. I'm very new to Android.

I'm thinking about creating an application that uses multiple databases. The user would be able to specify the name of the database to be created, and select which database to use from those existing in the /databases/ directory.

Is this possible in android?

A: 

Yes, all you described is possible. The following samplet illustrate how to do this

/**
 * Implement database helper by extending SQLiteOpenHelper, 
 * as described by dev.guide
 */
public class DatabaseOpenHelper extends SQLiteOpenHelper {
DatabaseOpenHelper(Context ctx, String dbPath, int dbVersion) {
    super(ctx, dbPath, null, dbVersion);
   }
     // the rest of the class 
}


// in your code when your want to create DB - 
// just define desired database path/name and pass it as argument 
// to helper's constructor
String dbPathName = "/sdcard/path/to/somewhere_on_sdcard/mydb.db";
// Any number you choose, just make sure any time you modify DB structure to 
// increment this number
static int dbVersion = 123;

// Instantiate your helper. That's all
DatabaseOpenHelper dbHelper = 
   new DatabaseOpenHelper(context, dbPathName, dbVersion);
Vlad