android 中 SQLiteOpenHelper的封裝使用詳解
在android中常用存儲(chǔ)數(shù)據(jù)的基本就三種,sqlite,SharedPreferences,文件存儲(chǔ),其中針對(duì)于對(duì)象存儲(chǔ),使用sqlite比較多,因?yàn)榭梢詫?duì)其進(jìn)行增刪改查。本文主要講解SQLiteOpenHelper的封裝使用,代碼引用自https://github.com/iMeiji/Toutiao
具體使用
主要方法包括創(chuàng)建數(shù)據(jù)庫(kù)和數(shù)據(jù)庫(kù)的升級(jí)。
構(gòu)造函數(shù):包含三個(gè)參數(shù),context,name,factory,version
onCreate:主要?jiǎng)?chuàng)建了三張表單
getDatabase:這里其實(shí)可以獲取兩個(gè)數(shù)據(jù)庫(kù),分別是getWritableDatabase與getReadableDatabase,這兩者的區(qū)別不是特別大,都具有對(duì)數(shù)據(jù)庫(kù)的讀寫(xiě) 權(quán)限。
getWritableDatabase取得的實(shí)例是以讀寫(xiě)的方式打開(kāi)數(shù)據(jù)庫(kù),如果打開(kāi)的數(shù)據(jù)庫(kù)磁盤滿了,此時(shí)只能讀不能寫(xiě),此時(shí)調(diào)用了getWritableDatabase的實(shí)例,那么將會(huì)發(fā)生錯(cuò)誤(異常)
getReadableDatabase取得的實(shí)例是先調(diào)用getWritableDatabase以讀寫(xiě)的方式打開(kāi)數(shù)據(jù)庫(kù),如果數(shù)據(jù)庫(kù)的磁盤滿了,此時(shí)返回打開(kāi)失敗,繼而用getReadableDatabase的實(shí)例以只讀的方式去打開(kāi)數(shù)據(jù)庫(kù)
onUpgrade:主要用于數(shù)據(jù)庫(kù)的升級(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;
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android Kotlin使用SQLite案例詳解
- android中SQLite使用及特點(diǎn)
- android studio使用SQLiteOpenHelper()建立數(shù)據(jù)庫(kù)的方法
- Android Studio 通過(guò)登錄功能介紹SQLite數(shù)據(jù)庫(kù)的使用流程
- Android開(kāi)發(fā)之使用SQLite存儲(chǔ)數(shù)據(jù)的方法分析
- Android中SQLite 使用方法詳解
- Android使用SQLite數(shù)據(jù)庫(kù)的示例
- Android開(kāi)發(fā)中使用sqlite實(shí)現(xiàn)新聞收藏和取消收藏的功能
- Android內(nèi)置SQLite的使用詳細(xì)介紹
相關(guān)文章
Android編程實(shí)現(xiàn)自定義toast示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)自定義toast,結(jié)合簡(jiǎn)單實(shí)例形式分析了自定義布局toast核心實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01
Android自定義View基礎(chǔ)開(kāi)發(fā)之圖片加載進(jìn)度條
這篇文章主要介紹了Android自定義View基礎(chǔ)開(kāi)發(fā)之圖片加載進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
flutter實(shí)現(xiàn)點(diǎn)擊事件
這篇文章主要為大家詳細(xì)介紹了flutter實(shí)現(xiàn)點(diǎn)擊事件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08
Android 判斷所有字段是否已經(jīng)輸入的實(shí)例
今天小編就為大家分享一篇Android 判斷所有字段是否已經(jīng)輸入的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
解析Android中如何做到Service被關(guān)閉后又自動(dòng)啟動(dòng)的實(shí)現(xiàn)方法
本篇文章是對(duì)在Android中如何做到Service被關(guān)閉后又自動(dòng)啟動(dòng)的方法進(jìn)行了詳細(xì)的分析和介紹。需要的朋友參考下2013-05-05
Android EasyBarrage實(shí)現(xiàn)輕量級(jí)彈幕效果
本篇文章主要介紹了Android EasyBarrage實(shí)現(xiàn)輕量級(jí)彈幕效果,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
Android中分析Jetpack?Compose動(dòng)畫(huà)內(nèi)部的實(shí)現(xiàn)原理
這篇文章主要介紹了Android中分析Jetpack?Compose動(dòng)畫(huà)內(nèi)部的實(shí)現(xiàn)原理,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-09-09

