这是这个问题的后续问题。
我正在从 Android.com 阅读本教程,并在遵循教程的同时编写了此代码:
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";
}
}
当我将我的应用程序发布到 Google Play 商店并且用户安装它时,SongDbHelper.onCreate()
将自动调用它,并使用我的Song
表创建一个数据库。这个数据库是如何充满值的?我猜我需要在SongDbHelper.onCreate()
用数据填充它的方法中编写代码。这意味着我需要将数据存储在源代码中的某个地方。
我想将我的歌词存储在我的strings.xml
文件中,但这似乎是多余的。然后,歌词将在strings.xml
文件和数据库中。在这种情况下,我几乎不需要数据库,除非我需要调用SELECT
数据来过滤它。这真的是人们填充数据库的方式吗?
理想情况下,我想自己填写数据库,然后让用户从应用商店下载数据库。人们说我们做不到。这是真的吗?将数据放在 2 个地方(数据库和)似乎是一种浪费。 strings.xml
我可能有很多数据!
大多数人是否让他们的应用程序从互联网服务器下载数据库?我希望不必为所有已发布的应用程序配备 Internet 服务器,只需在 Google Play 上托管数据库即可。