Nuovo per Android. E 'possibile per l'utente di specificare il nome di un database da creare? Anche sull'utilizzo di più database

StackOverflow https://stackoverflow.com/questions/3604759

Domanda

Sono molto nuovo per Android.

Sto pensando di creare un'applicazione che utilizza più database. L'utente sarà in grado di specificare il nome del database da creare, e selezionare il database da utilizzare tra quelle esistenti in / database / directory.

Questo è possibile in Android?

È stato utile?

Soluzione

Si, tutto hai descritto è possibile. Il seguente samplet illustrano come fare questo

/**
 * 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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top