码迷,mamicode.com
首页 > 移动开发 > 详细

Base-Android快速开发框架(三)--数据存储之SQLite

时间:2015-11-12 10:04:57      阅读:387      评论:0      收藏:0      [点我收藏+]

标签:

  SQLite,是一款轻量级的关系型数据库,Android原生集成的一个数据库。具有轻量级、独立性、隔离性、安全性等特点。是Android做数据存储的必备知识之一。

  在实际的项目中,我们常用于一些对象的存储以及检索。曾经做过一个餐饮点餐系统,就是需要把所有的菜谱、分类等基础数据做本地缓存,这个时候如果你用上章介绍的SharedPreferences,简直就疯掉了。

数据需要做排序、筛选、检索、分页获取等。这个时候就是Sqlite的长处了。跟上章一样,不会有介绍基础的api使用,直接介绍Base里面应用的Sqlite Orm操作框架,如何高效、灵活的使用Sqlite。

先上一段在网上找的sqlite代码片段。

技术分享

似乎还不错,操作student对象。也封装了insert update等的操作。但是实际业务的app迭代开发,常由于业务需要,变化很大。假如此时student多了一个phone的属性,怎么破呢?

假设业务需求,又有老师的对象要加入,把代码拷贝一份么?好吧,大家自己想象。另外不建议在activity层自己出现原生的sql 语句操作,这样耦合度太高了。接下来SQLite orm就派上用场了,同样,先上一段代码。


public void example(){
UserDao userDao=new UserDao(mContext);
//craete user
User user=new User();
userDao.insert(user);
//update user
userDao.update(user);
//select user
user=userDao.get("id");
List<User> userList=userDao.find();
//delete
userDao.delete();
}


有人要说了你的UserDao是什么,不会也是上面的InserData()方法集合吧?上下UserDao类。
public class UserDao extends TemplateDAO<User> {

public UserDao(Context context) {
super(new DBHelper(context));
}
}
问题关键来了TemplateDAO,是一个泛型类,也是整个Orm框架的核心。使用了java里面的标注 反射技术、泛型技术。下面先给大家看下User类。
@SimpleTable(name = "t_user")
public class User implements Serializable {
private static final long serialVersionUID = 2365701157715369155L;

public static Integer REMBER_PASSWORD = 1;
public static Integer AUTO_LOGIN = 1;

@SimpleId
@SimpleColumn(name = "userId")
private String userId;// 用户id
@SimpleColumn(name = "password")
private String password;// 密码
@SimpleColumn(name = "createDate")
private String createDate;// 创建时间
@SimpleColumn(name = "headerPic")
private String headerPic;// 头像图片路径
@SimpleColumn(name = "nickName")
private String nickName;// 昵称
@SimpleColumn(name = "syllabusPic")
private String syllabusPic;// 课程表图片
@SimpleColumn(name = "userName")
private String userName;// 用户名
@SimpleColumn(name = "email")
private String email;// 邮箱地址
@SimpleColumn(name = "mobile")
private String mobile;// 手机号
@SimpleColumn(name = "remberPassword")
private Integer remberPassword;// 记住密码 1:记住,其它不记住
@SimpleColumn(name = "autoLogin")
private Integer autoLogin; // 自动登陆 1:自动登陆,其它不登陆
@SimpleColumn(name = "type")
private Integer type; // 1个人 2企业
@SimpleColumn(name = "deptId")
private Integer deptId;
@SimpleColumn(name = "freeReadCount")
private String freeReadCount;
@SimpleColumn(name = "remark")
private String remark;
@SimpleColumn(name = "remainingSum")
private String remainingSum;
@SimpleColumn(name = "userToken")
private String userToken;
}
这就是Java里面的标注,@SimpleId标注了主键,@SimpleColumn标注了字段,TemplateDAO里面会根据反射自动生成数据表,并提供基础的增删改查,hql查询方法。上下关键的TemplateDAO类。
public class TemplateDAO<T> implements BaseDao<T> {
private String TAG = "SimpleSqlite";
private SQLiteOpenHelper dbHelper;
private String tableName;
private String idColumn;
private Class<T> clazz;
private List<Field> allFields;

public TemplateDAO(SQLiteOpenHelper dbHelper) {
this.dbHelper = dbHelper;
this.clazz = (Class)((ParameterizedType)super.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
if(this.clazz.isAnnotationPresent(SimpleTable.class)) {
SimpleTable field = (SimpleTable)this.clazz.getAnnotation(SimpleTable.class);
this.tableName = field.name();
}

this.allFields = TableHelper.joinFields(this.clazz.getDeclaredFields(), this.clazz.getSuperclass().getDeclaredFields());
Iterator var3 = this.allFields.iterator();

while(var3.hasNext()) {
Field field1 = (Field)var3.next();
if(field1.isAnnotationPresent(SimpleId.class)) {
SimpleColumn column = (SimpleColumn)field1.getAnnotation(SimpleColumn.class);
this.idColumn = column.name();
break;
}
}

Log.d(this.TAG, "clazz:" + this.clazz + " tableName:" + this.tableName + " idColumn:" + this.idColumn);
}

public SQLiteOpenHelper getDbHelper() {
return this.dbHelper;
}

public T get(int id) {
String selection = this.idColumn + " = ?";
String[] selectionArgs = new String[]{Integer.toString(id)};
Log.d(this.TAG, "[get]: select * from " + this.tableName + " where " + this.idColumn + " = \‘" + id + "\‘");
List list = this.find((String[])null, selection, selectionArgs, (String)null, (String)null, (String)null, (String)null);
return list != null && list.size() > 0?list.get(0):null;
}

public T get(String id) {
String selection = this.idColumn + " = ?";
String[] selectionArgs = new String[]{id};
Log.d(this.TAG, "[get]: select * from " + this.tableName + " where " + this.idColumn + " = \‘" + id + "\‘");
List list = this.find((String[])null, selection, selectionArgs, (String)null, (String)null, (String)null, (String)null);
return list != null && list.size() > 0?list.get(0):null;
}

public List<T> rawQuery(String sql, String[] selectionArgs) {
Log.d(this.TAG, "[rawQuery]: " + sql);
ArrayList list = new ArrayList();
SQLiteDatabase db = null;
Cursor cursor = null;

try {
db = this.dbHelper.getReadableDatabase();
cursor = db.rawQuery(sql, selectionArgs);
this.getListFromCursor(list, cursor);
} catch (Exception var10) {
Log.e(this.TAG, "[rawQuery] from DB Exception.");
var10.printStackTrace();
} finally {
if(cursor != null) {
cursor.close();
}

if(db != null) {
db.close();
}

}

return list;
}

public boolean isExist(String sql, String[] selectionArgs) {
Log.d(this.TAG, "[isExist]: " + sql);
SQLiteDatabase db = null;
Cursor cursor = null;

try {
db = this.dbHelper.getReadableDatabase();
cursor = db.rawQuery(sql, selectionArgs);
if(cursor.getCount() <= 0) {
return false;
}
} catch (Exception var9) {
Log.e(this.TAG, "[isExist] from DB Exception.");
var9.printStackTrace();
return false;
} finally {
if(cursor != null) {
cursor.close();
}

if(db != null) {
db.close();
}

}

return true;
}

public List<T> find() {
return this.find((String[])null, (String)null, (String[])null, (String)null, (String)null, (String)null, (String)null);
}

public List<T> find(String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) {
Log.d(this.TAG, "[find]");
ArrayList list = new ArrayList();
SQLiteDatabase db = null;
Cursor cursor = null;

try {
db = this.dbHelper.getReadableDatabase();
cursor = db.query(this.tableName, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
this.getListFromCursor(list, cursor);
} catch (Exception var15) {
Log.e(this.TAG, "[find] from DB Exception");
var15.printStackTrace();
} finally {
if(cursor != null) {
cursor.close();
}

if(db != null) {
db.close();
}

}

return list;
}

private void getListFromCursor(List<T> list, Cursor cursor) throws IllegalAccessException, InstantiationException {
label77:
while(cursor.moveToNext()) {
Object entity = this.clazz.newInstance();
Iterator var5 = this.allFields.iterator();

while(true) {
while(true) {
while(true) {
Field field;
Class fieldType;
int c;
do {
SimpleColumn column;
do {
if(!var5.hasNext()) {
list.add(entity);
continue label77;
}

field = (Field)var5.next();
column = null;
} while(!field.isAnnotationPresent(SimpleColumn.class));

column = (SimpleColumn)field.getAnnotation(SimpleColumn.class);
field.setAccessible(true);
fieldType = field.getType();
c = cursor.getColumnIndex(column.name());
} while(c < 0);

if(Integer.TYPE != fieldType && Integer.class != fieldType) {
if(String.class == fieldType) {
field.set(entity, cursor.getString(c));
} else if(Long.TYPE != fieldType && Long.class != fieldType) {
if(Float.TYPE != fieldType && Float.class != fieldType) {
if(Short.TYPE != fieldType && Short.class != fieldType) {
if(Double.TYPE != fieldType && Double.class != fieldType) {
if(Blob.class == fieldType) {
field.set(entity, cursor.getBlob(c));
} else if(Character.TYPE == fieldType) {
String fieldValue = cursor.getString(c);
if(fieldValue != null && fieldValue.length() > 0) {
                             field.set(entity, Character.valueOf(fieldValue.charAt(0)));
}
}
} else {
field.set(entity, Double.valueOf(cursor.getDouble(c)));
}
} else {
field.set(entity, Short.valueOf(cursor.getShort(c)));
}
} else {
field.set(entity, Float.valueOf(cursor.getFloat(c)));
}
} else {
field.set(entity, Long.valueOf(cursor.getLong(c)));
}
} else {
field.set(entity, Integer.valueOf(cursor.getInt(c)));
}
}
}
}
}

}

public long insert(T entity) {
Log.d(this.TAG, "[insert]: inset into " + this.tableName + " " + entity.toString());
SQLiteDatabase db = null;

try {
db = this.dbHelper.getWritableDatabase();
ContentValues e = new ContentValues();
this.setContentValues(entity, e, "create");
long row = db.insert(this.tableName, (String)null, e);
long var7 = row;
return var7;
} catch (Exception var11) {
Log.d(this.TAG, "[insert] into DB Exception.");
var11.printStackTrace();
} finally {
if(db != null) {
db.close();
}

}

return 0L;
}

public void delete(int id) {
SQLiteDatabase db = this.dbHelper.getWritableDatabase();
String where = this.idColumn + " = ?";
String[] whereValue = new String[]{Integer.toString(id)};
Log.d(this.TAG, "[delete]: delelte from " + this.tableName + " where " + where.replace("?", String.valueOf(id)));
db.delete(this.tableName, where, whereValue);
db.close();
}

public void delete(String id) {
this.delete(Integer.parseInt(id));
}

public void delete(String where, String[] whereValue) {
SQLiteDatabase db = this.dbHelper.getWritableDatabase();
Log.d(this.TAG, "[delete]: delelte from " + this.tableName + " where " + where + "=" + whereValue);
db.delete(this.tableName, where, whereValue);
db.close();
}

public void delete() {
SQLiteDatabase db = this.dbHelper.getWritableDatabase();
db.delete(this.tableName, (String)null, (String[])null);
db.close();
}

public void delete(Integer... ids) {
if(ids.length > 0) {
StringBuffer sb = new StringBuffer();

for(int db = 0; db < ids.length; ++db) {
sb.append(‘?‘).append(‘,‘);
}

sb.deleteCharAt(sb.length() - 1);
SQLiteDatabase var5 = this.dbHelper.getWritableDatabase();
String sql = "delete from " + this.tableName + " where " + this.idColumn + " in (" + sb + ")";
Log.d(this.TAG, "[delete]: " + sql);
var5.execSQL(sql, ids);
var5.close();
}

}

public void delete(String... ids) {
if(ids.length > 0) {
StringBuffer sb = new StringBuffer();

for(int db = 0; db < ids.length; ++db) {
sb.append(‘?‘).append(‘,‘);
}

sb.deleteCharAt(sb.length() - 1);
SQLiteDatabase var5 = this.dbHelper.getWritableDatabase();
String sql = "delete from " + this.tableName + " where " + this.idColumn + " in (" + sb + ")";
Log.d(this.TAG, "[delete]: " + sql);
var5.execSQL(sql, ids);
var5.close();
}

}

 

 public void update(T entity) {
SQLiteDatabase db = null;

try {
db = this.dbHelper.getWritableDatabase();
ContentValues e = new ContentValues();
this.setContentValues(entity, e, "update");
String where = this.idColumn + " = ?";
String id = e.get(this.idColumn).toString().trim();
e.remove(this.idColumn);
Log.d(this.TAG, "[update]: update " + this.tableName + " where " + where.replace("?", id));
String[] whereValue = new String[]{id};
db.update(this.tableName, e, where, whereValue);
} catch (Exception var10) {
Log.d(this.TAG, "[update] DB Exception.");
var10.printStackTrace();
} finally {
if(db != null) {
db.close();
}

}

}

  private void setContentValues(T entity, ContentValues cv, String type) throws IllegalAccessException {
Iterator var5 = this.allFields.iterator();

while(true) {
Field field;
SimpleColumn column;
Object fieldValue;
SimpleId id;
do {
do {
do {
if(!var5.hasNext()) {
return;
}

field = (Field)var5.next();
} while(!field.isAnnotationPresent(SimpleColumn.class));

column = (SimpleColumn)field.getAnnotation(SimpleColumn.class);
field.setAccessible(true);
fieldValue = field.get(entity);
} while(fieldValue == null);

id = (SimpleId)field.getAnnotation(SimpleId.class);
} while("create".equals(type) && field.isAnnotationPresent(SimpleId.class) && id != null && id.auto());

cv.put(column.name(), fieldValue.toString());
}
}

public List<Map<String, String>> query2MapList(String sql, String[] selectionArgs) {
Log.d(this.TAG, "[query2MapList]: " + sql);
SQLiteDatabase db = null;
Cursor cursor = null;
ArrayList retList = new ArrayList();

try {
db = this.dbHelper.getReadableDatabase();
cursor = db.rawQuery(sql, selectionArgs);

while(cursor.moveToNext()) {
HashMap e = new HashMap();
String[] var10;
int var9 = (var10 = cursor.getColumnNames()).length;

for(int var8 = 0; var8 < var9; ++var8) {
String columnName = var10[var8];
e.put(columnName.toLowerCase(), cursor.getString(cursor.getColumnIndex(columnName)));
}

retList.add(e);
}
} catch (Exception var14) {
Log.e(this.TAG, "[query2MapList] from DB exception");
var14.printStackTrace();
} finally {
if(cursor != null) {
cursor.close();
}

if(db != null) {
db.close();
}

}

return retList;
}

public void execSql(String sql, Object[] selectionArgs) {
SQLiteDatabase db = null;
Log.d(this.TAG, "[execSql]: " + sql);

try {
db = this.dbHelper.getWritableDatabase();
if(selectionArgs == null) {
db.execSQL(sql);
} else {
db.execSQL(sql, selectionArgs);
}
} catch (Exception var8) {
Log.e(this.TAG, "[execSql] DB exception.");
var8.printStackTrace();
} finally {
if(db != null) {
db.close();
}

}

}
}
回到上面的问题,如果Students多了一个字段怎么办?那么实体模型加多一个phone字段和标注,多了个teacher对象怎么办。建一个teacher对象 和一个teacherDao继承TemplateDAO。似乎好像一切都很顺利,然而当需要对象连表查询、当数据库升级后兼容旧数据等怎么办呢?诚邀大神加入... ...

Base-Android快速开发框架(三)--数据存储之SQLite

标签:

原文地址:http://www.cnblogs.com/huangjunbin/p/4957963.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!