您好,登錄后才能下訂單哦!
在android中常用存儲數據的基本就三種,sqlite,SharedPreferences,文件存儲,其中針對于對象存儲,使用sqlite比較多,因為可以對其進行增刪改查。本文主要講解SQLiteOpenHelper的封裝使用,代碼引用自https://github.com/iMeiji/Toutiao
具體使用
主要方法包括創建數據庫和數據庫的升級。
構造函數:包含三個參數,context,name,factory,version
onCreate:主要創建了三張表單
getDatabase:這里其實可以獲取兩個數據庫,分別是getWritableDatabase與getReadableDatabase,這兩者的區別不是特別大,都具有對數據庫的讀寫 權限。
getWritableDatabase取得的實例是以讀寫的方式打開數據庫,如果打開的數據庫磁盤滿了,此時只能讀不能寫,此時調用了getWritableDatabase的實例,那么將會發生錯誤(異常)
getReadableDatabase取得的實例是先調用getWritableDatabase以讀寫的方式打開數據庫,如果數據庫的磁盤滿了,此時返回打開失敗,繼而用getReadableDatabase的實例以只讀的方式去打開數據庫
onUpgrade:主要用于數據庫的升級,這里面
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);//刪除表中的數據 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添加初始化數據
add插入到表中
query查詢特定數據
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; } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。