This is a follow-up question to this question.
I was reading this tutorial from Android.com, and I made this code while following the tutorial:
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
public final class SongDbHelper extends SQLiteOpenHelper {
// If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "Song.db";
private static final String TEXT_TYPE = " TEXT";
private static final String PRIMARY_KEY_TYPE = " INTEGER PRIMARY KEY";
private static final String COMMA_SEP = ",";
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + SongEntry.TABLE_NAME + " (" +
SongEntry._ID + PRIMARY_KEY_TYPE + COMMA_SEP +
SongEntry.COLUMN_NAME_SONG_TITLE + TEXT_TYPE + COMMA_SEP +
SongEntry.COLUMN_NAME_ENGLISH_LYRICS + TEXT_TYPE + COMMA_SEP +
SongEntry.COLUMN_NAME_ROMAJI_LYRICS + TEXT_TYPE + COMMA_SEP +
SongEntry.COLUMN_NAME_KANJI_LYRICS + TEXT_TYPE + COMMA_SEP +
SongEntry.COLUMN_NAME_INFO + TEXT_TYPE +
" )";
private static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + SongEntry.TABLE_NAME;
public SongDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade policy is
// to simply to discard the data and start over
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
public static abstract class SongEntry implements BaseColumns {
public static final String TABLE_NAME = "Song";
public static final String COLUMN_NAME_SONG_TITLE = "song_title";
public static final String COLUMN_NAME_ENGLISH_LYRICS = "english_lyrics";
public static final String COLUMN_NAME_ROMAJI_LYRICS = "romaji_lyrics";
public static final String COLUMN_NAME_KANJI_LYRICS = "kanji_lyrics";
public static final String COLUMN_NAME_INFO = "info";
}
}
When I publish my app to the Google Play store, and a user installs it, SongDbHelper.onCreate()
will automatically be called, creating a database with my Song
table. How does this database become filled with values? I am guessing that I need to write code in the SongDbHelper.onCreate()
method that fills it with data. That means I need to store the data in the source code somewhere.
I thought of storing my song lyrics in my strings.xml
file, but it seems redundant. Then, the lyrics will be in the strings.xml
file and the database. I almost don't need a database in that case, unless I need to call SELECT
on the data to filter it. Is this really the way people fill the database?
Ideally, I would like to fill the database on my own, and then have users download the database from the app store. People have said that we can't do that. Is that really true? It seems like a waste to have the data in 2 places (the database and strings.xml
). I might have a lot of data!
Do most people have their app download the database from an internet server? I would like to not have to have an internet server for all my published apps, and just host the database on Google Play.