Android SQLite?????汾??????????
???????????? ???????[ 2015/2/2 10:58:20 ] ????????????? SQL Android
????????SQL.java????????
public class SQL {
public static final String T_FAVORITE = "favorite";
public static final String CREATE_TABLE_FAVORITE =
"CREATE TABLE IF NOT EXISTS " + T_FAVORITE + "(" +
"id VARCHAR PRIMARY KEY?? " +
"title VARCHAR?? " +
"url VARCHAR?? " +
"createDate VARCHAR " +
")";
}
??????2????V2.0 : DATABASE_VERSION = 1001 ??favorite?????1??deleted???
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "mall.db";
private static final int DATABASE_VERSION = 1001;
private static DBHelper instance = null;
public DBHelper(Context context) {
super(context?? DATABASE_NAME?? null?? DATABASE_VERSION);
}
public synchronized static DBHelper getInstance(Context context) {
if (instance == null) {
instance = new DBHelper(context);
}
return instance;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL.CREATE_TABLE_FAVORITE);
// ??????????汾????????????????????
// ??????FIRST_DATABASE_VERSION??????????????????汾??С
final int FIRST_DATABASE_VERSION = 1000;
onUpgrade(db?? FIRST_DATABASE_VERSION?? DATABASE_VERSION);
}
@Override
public void onUpgrade(SQLiteDatabase db?? int oldVersion?? int newVersion) {
// ???for????汾?????????
for (int i = oldVersion; i < newVersion; i++) {
switch (i) {
case 1000:
upgradeToVersion1001(db);
break;
default:
break;
}
}
}
private void upgradeToVersion1001(SQLiteDatabase db){
// favorite??????1?????
String sql1 = "ALTER TABLE "+SQL.T_FAVORITE+" ADD COLUMN deleted VARCHAR";
db.execSQL(sql1);
}
}
??????3????V3.0 : DATABASE_VERSION = 1002 ??favorite?????message??type???
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "mall.db";
private static final int DATABASE_VERSION = 1002;
private static DBHelper instance = null;
public DBHelper(Context context) {
super(context?? DATABASE_NAME?? null?? DATABASE_VERSION);
}
public synchronized static DBHelper getInstance(Context context) {
if (instance == null) {
instance = new DBHelper(context);
}
return instance;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL.CREATE_TABLE_FAVORITE);
// ??????????汾????????????????????
// ??????FIRST_DATABASE_VERSION??????????????????汾??С
final int FIRST_DATABASE_VERSION = 1000;
onUpgrade(db?? FIRST_DATABASE_VERSION?? DATABASE_VERSION);
}
@Override
public void onUpgrade(SQLiteDatabase db?? int oldVersion?? int newVersion) {
// ???for????汾?????????
for (int i = oldVersion; i < newVersion; i++) {
switch (i) {
case 1000:
upgradeToVersion1001(db);
break;
case 1001:
upgradeToVersion1002(db);
break;
default:
break;
}
}
}
private void upgradeToVersion1001(SQLiteDatabase db){
// favorite??????1?????
String sql1 = "ALTER TABLE "+SQL.T_FAVORITE+" ADD COLUMN deleted VARCHAR";
db.execSQL(sql1);
}
private void upgradeToVersion1002(SQLiteDatabase db){
// favorite??????2????Σ???????????????????????μ??sqlite???????????????????????
String sql1 = "ALTER TABLE "+SQL.T_FAVORITE+" ADD COLUMN message VARCHAR";
String sql2 = "ALTER TABLE "+SQL.T_FAVORITE+" ADD COLUMN type VARCHAR";
db.execSQL(sql1);
db.execSQL(sql2);
}
}
????????????????v1.0??????v3.0??????v2.0??????3.0??????v3.0?????????????v3.0????????????????????????e???????sqlite????????????????Щ???????????????????д????£???????????????£???onUpgrade???????????д??????????????????????????????鷳???
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11