在android中常用存储数据的基本就三种,sqlite,SharedPreferences,文件存储,其中针对于对象存储,使用sqlite比较多,因为可以对其进行增删改查。本文主要讲解SQLiteOpenHelper的封装使用,代码引用自https://github.com/iMeiji/Toutiao
具体使用
主要方法包括创建数据库和数据库的升级。
构造函数:包含三个参数,context,name,factory,version
onCreate:主要创建了三张表单
getDatabase:这里其实可以获取两个数据库,分别是getWritableDatabase与getReadableDatabase,这两者的区别不是特别大,都具有对数据库的读写 权限。
getWritableDatabase取得的实例是以读写的方式打开数据库,如果打开的数据库磁盘满了,此时只能读不能写,此时调用了getWritableDatabase的实例,那么将会发生错误(异常)
getReadableDatabase取得的实例是先调用getWritableDatabase以读写的方式打开数据库,如果数据库的磁盘满了,此时返回打开失败,继而用getReadableDatabase的实例以只读的方式去打开数据库
onUpgrade:主要用于数据库的升级,这里面
代码语言:javascript复制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查询特定数据
代码语言:javascript复制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;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助。