我对这种方法感到困惑,并不明白这个目的。
我发现Android 4.4的默认台钟应用程序已经注意到一些奇怪的东西。在ClockDatabaseHelper
类中,在数据库中创建了几个表。
// Database and table names
static final String DATABASE_NAME = "alarms.db";
static final String OLD_ALARMS_TABLE_NAME = "alarms";
static final String ALARMS_TABLE_NAME = "alarm_templates";
static final String INSTANCES_TABLE_NAME = "alarm_instances";
static final String CITIES_TABLE_NAME = "selected_cities";
所以第一个字符串是清楚的。第二个字符串为什么命名为OLD_ALARMS并且表的实名是警报。它为什么有这样的名字?什么是真正的puprose。 接下来,第三个字符串的值为“alarm_templates”...为什么? “模板”代表什么词?它为什么有这样的名字? 实例表是针对当前的报警状态...但是我仍然不清楚为什么我们不能直接写入报警表。 请解释一下做这些事情的目标是什么?
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion) {
LogUtils.v("Upgrading alarms database from version "
+ oldVersion + " to " + currentVersion);
if (oldVersion <= VERSION_6) {
// These were not used in DB_VERSION_6, so we can just drop them.
db.execSQL("DROP TABLE IF EXISTS " + INSTANCES_TABLE_NAME + ";");
db.execSQL("DROP TABLE IF EXISTS " + CITIES_TABLE_NAME + ";");
// Create new alarms table and copy over the data
createAlarmsTable(db);
createInstanceTable(db);
createCitiesTable(db);
LogUtils.i("Copying old alarms to new table");
String[] OLD_TABLE_COLUMNS = {
"_id",
"hour",
"minutes",
"daysofweek",
"enabled",
"vibrate",
"message",
"alert",
};
Cursor cursor = db.query(OLD_ALARMS_TABLE_NAME, OLD_TABLE_COLUMNS,
null, null, null, null, null);
Calendar currentTime = Calendar.getInstance();
while (cursor.moveToNext()) {
Alarm alarm = new Alarm();
alarm.id = cursor.getLong(0);
alarm.hour = cursor.getInt(1);
alarm.minutes = cursor.getInt(2);
alarm.daysOfWeek = new DaysOfWeek(cursor.getInt(3));
alarm.enabled = cursor.getInt(4) == 1;
alarm.vibrate = cursor.getInt(5) == 1;
alarm.label = cursor.getString(6);
String alertString = cursor.getString(7);
if ("silent".equals(alertString)) {
alarm.alert = Alarm.NO_RINGTONE_URI;
} else {
alarm.alert = TextUtils.isEmpty(alertString) ? null : Uri.parse(alertString);
}
// Save new version of alarm and create alarminstance for it
db.insert(ALARMS_TABLE_NAME, null, Alarm.createContentValues(alarm));
if (alarm.enabled) {
AlarmInstance newInstance = alarm.createInstanceAfter(currentTime);
db.insert(INSTANCES_TABLE_NAME, null,
AlarmInstance.createContentValues(newInstance));
}
}
cursor.close();
LogUtils.i("Dropping old alarm table");
db.execSQL("DROP TABLE IF EXISTS " + OLD_ALARMS_TABLE_NAME + ";");
}
}
以下是使用OLD_ALARMS_TABLE_NAME的方法 - onUpgrade我们只是将旧表复制到new,但我们从未写信给她。请解释这里发生的事情。谢谢
修改
感谢 CL。,在写作期间我得到了它。
这是用于更新旧数据库表,这里是来自旧应用程序的代码
private static class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "alarms.db";
private static final int DATABASE_VERSION = 5;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE alarms (" +
"_id INTEGER PRIMARY KEY," +
"hour INTEGER, " +
"minutes INTEGER, " +
"daysofweek INTEGER, " +
"alarmtime INTEGER, " +
"enabled INTEGER, " +
"vibrate INTEGER, " +
"message TEXT, " +
"alert TEXT);");
// insert default alarms
String insertMe = "INSERT INTO alarms " +
"(hour, minutes, daysofweek, alarmtime, enabled, vibrate, message, alert) " +
"VALUES ";
db.execSQL(insertMe + "(7, 0, 127, 0, 0, 1, '', '');");
db.execSQL(insertMe + "(8, 30, 31, 0, 0, 1, '', '');");
db.execSQL(insertMe + "(9, 00, 0, 0, 0, 1, '', '');");
}
所以这里的名称是在insert语句中硬编码的。我认为这是不好的方法,首先,常量值的所有变量都不是常量变量。这里是sql openhelper是嵌套的静态类。我不知道,也许我错了。