开发者

New to Android. Is it possible for the user to specify the name of a Database to be created? Also on using multiple databases

I'm very new to Android.

I'm thinking about creating an application that uses multiple databases.开发者_如何学JAVA 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?


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜