欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android 中 SQLiteOpenHelper的封裝使用詳解

 更新時間:2018年02月28日 08:34:41   作者:上官若楓  
這篇文章主要介紹了android 中 SQLiteOpenHelper的封裝使用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

在android中常用存儲數(shù)據(jù)的基本就三種,sqlite,SharedPreferences,文件存儲,其中針對于對象存儲,使用sqlite比較多,因為可以對其進(jìn)行增刪改查。本文主要講解SQLiteOpenHelper的封裝使用,代碼引用自https://github.com/iMeiji/Toutiao

具體使用

主要方法包括創(chuàng)建數(shù)據(jù)庫和數(shù)據(jù)庫的升級。

構(gòu)造函數(shù):包含三個參數(shù),context,name,factory,version

onCreate:主要創(chuàng)建了三張表單

getDatabase:這里其實可以獲取兩個數(shù)據(jù)庫,分別是getWritableDatabase與getReadableDatabase,這兩者的區(qū)別不是特別大,都具有對數(shù)據(jù)庫的讀寫 權(quán)限。

getWritableDatabase取得的實例是以讀寫的方式打開數(shù)據(jù)庫,如果打開的數(shù)據(jù)庫磁盤滿了,此時只能讀不能寫,此時調(diào)用了getWritableDatabase的實例,那么將會發(fā)生錯誤(異常)

getReadableDatabase取得的實例是先調(diào)用getWritableDatabase以讀寫的方式打開數(shù)據(jù)庫,如果數(shù)據(jù)庫的磁盤滿了,此時返回打開失敗,繼而用getReadableDatabase的實例以只讀的方式去打開數(shù)據(jù)庫

onUpgrade:主要用于數(shù)據(jù)庫的升級,這里面

public class DatabaseHelper extends SQLiteOpenHelper {

  private static final String DB_NAME = "Toutiao";
  private static final int DB_VERSION = 5;
  private static final String CLEAR_TABLE_DATA = "delete from ";
  private static final String DROP_TABLE = "drop table if exists ";
  private static DatabaseHelper instance = null;
  private static SQLiteDatabase db = null;

  private DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, name, factory, version);
  }

  private static synchronized DatabaseHelper getInstance() {
    if (instance == null) {
      instance = new DatabaseHelper(InitApp.AppContext, DB_NAME, null, DB_VERSION);
    }
    return instance;
  }

  public static synchronized SQLiteDatabase getDatabase() {
    if (db == null) {
      db = getInstance().getWritableDatabase();
    }
    return db;
  }

  public static synchronized void closeDatabase() {
    if (db != null) {
      db.close();
    }
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
    db.execSQL(NewsChannelTable.CREATE_TABLE);
    db.execSQL(MediaChannelTable.CREATE_TABLE);
    db.execSQL(SearchHistoryTable.CREATE_TABLE);
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    switch (oldVersion) {
      case 1:
        db.execSQL(MediaChannelTable.CREATE_TABLE);
        break;
      case 2:
        db.execSQL(CLEAR_TABLE_DATA + NewsChannelTable.TABLENAME);//刪除表中的數(shù)據(jù)
        break;
      case 3:
        ContentValues values = new ContentValues();
        values.put(NewsChannelTable.ID, "");
        values.put(NewsChannelTable.NAME, "推薦");
        values.put(NewsChannelTable.IS_ENABLE, 0);
        values.put(NewsChannelTable.POSITION, 46);
        db.insert(NewsChannelTable.TABLENAME, null, values);//新建表
        break;
      case 4:
        db.execSQL(SearchHistoryTable.CREATE_TABLE);
        break;
    }
  }
}

表操作的封裝

addInitData添加初始化數(shù)據(jù)

add插入到表中

query查詢特定數(shù)據(jù)

public class NewsChannelDao {

  private SQLiteDatabase db;

  public NewsChannelDao() {
    this.db = DatabaseHelper.getDatabase();
  }

  public void addInitData() {
    String categoryId[] = InitApp.AppContext.getResources().getStringArray(R.array.mobile_news_id);
    String categoryName[] = InitApp.AppContext.getResources().getStringArray(R.array.mobile_news_name);
    for (int i = 0; i < 8; i++) {
      add(categoryId[i], categoryName[i], Constant.NEWS_CHANNEL_ENABLE, i);
    }
    for (int i = 8; i < categoryId.length; i++) {
      add(categoryId[i], categoryName[i], Constant.NEWS_CHANNEL_DISABLE, i);
    }
  }

  public boolean add(String channelId, String channelName, int isEnable, int position) {
    ContentValues values = new ContentValues();
    values.put(NewsChannelTable.ID, channelId);
    values.put(NewsChannelTable.NAME, channelName);
    values.put(NewsChannelTable.IS_ENABLE, isEnable);
    values.put(NewsChannelTable.POSITION, position);
    long result = db.insert(NewsChannelTable.TABLENAME, null, values);
    return result != -1;
  }

  public List<NewsChannelBean> query(int isEnable) {
    Cursor cursor = db.query(NewsChannelTable.TABLENAME, null, NewsChannelTable.IS_ENABLE + "=?",
        new String[]{isEnable + ""}, null, null, null);
    List<NewsChannelBean> list = new ArrayList<>();
    while (cursor.moveToNext()) {
      NewsChannelBean bean = new NewsChannelBean();
      bean.setChannelId(cursor.getString(NewsChannelTable.ID_ID));
      bean.setChannelName(cursor.getString(NewsChannelTable.ID_NAME));
      bean.setIsEnable(cursor.getInt(NewsChannelTable.ID_ISENABLE));
      bean.setPosition(cursor.getInt(NewsChannelTable.ID_POSITION));
      list.add(bean);
    }
    cursor.close();
    return list;
  }

  public List<NewsChannelBean> queryAll() {
    Cursor cursor = db.query(NewsChannelTable.TABLENAME, null, null, null, null, null, null);
    List<NewsChannelBean> list = new ArrayList<>();
    while (cursor.moveToNext()) {
      NewsChannelBean bean = new NewsChannelBean();
      bean.setChannelId(cursor.getString(NewsChannelTable.ID_ID));
      bean.setChannelName(cursor.getString(NewsChannelTable.ID_NAME));
      bean.setIsEnable(cursor.getInt(NewsChannelTable.ID_ISENABLE));
      bean.setPosition(cursor.getInt(NewsChannelTable.ID_POSITION));
      list.add(bean);
    }
    cursor.close();
    return list;
  }

  public void updateAll(List<NewsChannelBean> list) {
  }

  public boolean removeAll() {
    int result = db.delete(NewsChannelTable.TABLENAME, null, null);
    return result != -1;
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論