1 /** 2 * 实现对表的创建、更新、变更列名操作 3 * 4 * 5 */ 6 public class SQLiteHelper extends SQLiteOpenHelper { 7 public static final String DB_NAME = "historyDB"; 8 public static final String TB_NAME = "historyTB"; 9 10 public SQLiteHelper(Context context) 11 { 12 super(context, DB_NAME, null, 1); 13 } 14 15 /** 16 * 创建新表 17 */ 18 @Override 19 public void onCreate(SQLiteDatabase db) { 20 db.execSQL("CREATE TABLE IF NOT EXISTS " + 21 TB_NAME + "(" + 22 //HistoryBean.ID + " integer primary key," + 23 HistoryBean.URL + " varchar," + 24 HistoryBean.TIME + " integer,"+ 25 HistoryBean.NAME + " varchar"+ 26 ")"); 27 } 28 29 /** 30 * 当检测与前一次创建数据库版本不一样时,先删除表再创建新表 31 */ 32 @Override 33 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 34 db.execSQL("DROP TABLE IF EXISTS " + TB_NAME); 35 onCreate(db); 36 } 37 38 /** 39 * 变更列名 40 * @param db 41 * @param oldColumn 42 * @param newColumn 43 * @param typeColumn 44 */ 45 public void updateColumn(SQLiteDatabase db, String oldColumn, String newColumn, String typeColumn){ 46 try{ 47 db.execSQL("ALTER TABLE " + 48 TB_NAME + " CHANGE " + 49 oldColumn + " "+ newColumn + 50 " " + typeColumn 51 ); 52 }catch(Exception e){ 53 e.printStackTrace(); 54 } 55 } 56 57 }