還有其他方法嗎?
是的,我個人使用一種僞/想/需要的模式,並在短期比較這對作爲需要什麼實際存在的列,表創建索引(表索引)/加(列)。
因此,我不使用版本和onUpgrade
是空的。
例如這些是在我的SQLiteOpenhelper子類中的核心方法: -
@Override
public void onCreate(SQLiteDatabase db) {
usable = this.onExpand(db,false);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
}
public boolean onExpand(SQLiteDatabase db, boolean buildandexpand) {
boolean rv = true;
if (db == null) {
db = instance.getWritableDatabase();
}
// Is the Database definition valid to use?
if (DBConstants.cardonsier.isDBDatabaseUsable()) {
ArrayList<String> buildsql = DBConstants.cardonsier.generateDBBuildSQL(db);
// Anything to build
if (!buildsql.isEmpty()) {
// YES so build the Database
DBConstants.cardonsier.actionDBBuildSQL(db);
}
if (buildandexpand) {
ArrayList<String> altersql = DBConstants.cardonsier.generateDBAlterSQL(db);
if (!altersql.isEmpty()) {
DBConstants.cardonsier.actionDBAlterSQL(db);
}
}
}
else {
rv = false;
}
return rv;
}
DBConstants.cardoniser指DBDatabase對象定義爲: -
static final DBDatabase cardonsier = new DBDatabase(DATABASENAME,
new ArrayList<>(Arrays.asList(
DBCardsTableConstants.CARDS,
DBUsertableConstants.USERS,
DBCardUserLinkTableConstants.CARDUSERLINKS,
DBPreftableConstants.PREFS,
DBPayeestableContants.PAYEES,
DBCategoriestableConstants.CATEGORIES,
DBCardCategoryLinkTableConstants.CARDCATEGORYLINKS,
DBCardPayeeLinkTableConstants.CARDPAYEELINKS,
DBTransactionsTableConstants.TRANSACTIONS,
DBCardTypesTableConstants.CARDTYPES
))
);
即DBTABLE對象的列表,他們包括DBColumn對象,例如: -
static final DBTable CARDS = new DBTable(CARDS_TABLE,
new ArrayList<>(Arrays.asList(
CARDID,
CARDOWNER,
CARDNAME,
CARDTYPEREF,
CARDNUMBER,
CARDCVVCODE,
CARDPIN,
CARDNOTES,
CARDEXPIRYDATE,
CARDNAMEONCARD
))
);
列正在定義如: -
static final DBColumn CARDID = new DBColumn(true);
static final DBColumn CARDOWNER = new DBColumn(
CARDOWNER_COL,
SQLINTEGER,
false,
"0"
);
static final DBColumn CARDNAME = new DBColumn(
CARDNAME_COL,
SQLTEXT,
false,
""
);
static final DBColumn CARDTYPEREF = new DBColumn(
CARDTYPEREF_COL,
SQLINTEGER,
false,
"0"
);
注意!第一列CARDID使用標準_id列的快捷鍵構造函數。
要添加新列,只需定義DBColumn並在DBTable定義中包括DBColumn即可。在新列的情況下,onExpand
方法將通過DBDatabase的actionDBAlterSQL
方法添加列。新表需要上述內容以及DBDatabase定義中的表格,並通過actionDBBuildSQL
方法由onExpand
應用。當應用程序啓動時,也會調用。但是,true
正在通過,因此調用了actionDBAlterSQL
方法。
作爲示例中加入: -
private static final String CARDCOLOUR_COL = "cardcolour";
..........
static final DBColumn CARDCOLOUR = new DBColumn(
CARDCOLOUR_COL,
SQLINTEGER,
false,
Long.toString(0x00_00_00_00_00L)
// Flag, alpha, red, green, blue
// Flag used to inidcate if colour has been set
);
,然後加入CARDCOLOUR
到DBTable
定義爲每: -
static final DBTable CARDS = new DBTable(CARDS_TABLE,
new ArrayList<>(Arrays.asList(
CARDID,
CARDOWNER,
CARDNAME,
CARDTYPEREF,
CARDNUMBER,
CARDCVVCODE,
CARDPIN,
CARDNOTES,
CARDEXPIRYDATE,
CARDNAMEONCARD,
CARDCOLOUR //<<<<<<<<<<
))
);
結果(顯影時存在註釋記錄): -
09-22 08:30:26.802 2713-2713/? D/DBEXPAND: Expanding Database Schema Usability=true
09-22 08:30:26.803 2713-2713/? D/DBEXPAND: Build SQL is as follows:-
09-22 08:30:26.803 2713-2713/? D/DBEXPAND: ALTERSQL is as folows:-
09-22 08:30:26.807 2713-2713/? D/DBEXPAND: ALterSQL Line= ALTER TABLE cards ADD COLUMN cardcolour INTEGER DEFAULT 0 ;
生成的已填充表格如下所示: -
使用ALTER TABLE
與DEFAULT
值(DBColumn(第一第四參數是列名,第二類型,第三真實在主索引包括))填充爲現有行的新列。
這個我幫你。 https://stackoverflow.com/questions/8133597/android-upgrading-db-version-and-adding-new-table –