android dbutils的简化版详解编程语言

  
//复杂的可以使用dbutils的,这就来个仿dbutils的简化版的吧 
 
public class DAOHelper { 
 
public static final MapRowProcessor MAPROWPROCESSOR = new MapRowProcessor(); 
   
   
private String tableName;   //表名 
private String[] cols;   //列名 
   
   
public DAOHelper(String tableName, String[] columns) { 
this.tableName = tableName; 
this.cols = columns; 
} 
   
public List<Map<String, String>> query(String sqlWhere) { 
return query(sqlWhere,null); 
} 
   
   
public List<Map<String, String>> query(String sqlWhere,String[] sqlWhereArgs) { 
ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>(0); 
   
   
Cursor cursor = null; 
try { 
SQLiteDatabase database = DBOpenHelper.getWritableDatabase(); 
cursor = database.query(tableName, cols, sqlWhere, sqlWhereArgs, null, null, null); 
   
list.ensureCapacity(cursor.getCount()); 
   
while (cursor.moveToNext()) { 
list.add(MAPROWPROCESSOR.process(cursor)); 
} 
} catch (Exception e) { 
Logger.error("DAOHelper", "插入失败"); 
} finally { 
if (null != cursor) { 
cursor.close(); 
} 
} 
   
   
return list; 
} 
   
   
public int insert(List<Map<String, String>> list) { 
SQLiteDatabase database = DBOpenHelper.getWritableDatabase(); 
   
   
try { 
// 打开数据库 
database.beginTransaction(); 
for (Map<String, String> map : list) { 
ContentValues v = mapToContentValues(map); 
database.insert(tableName, null, v); 
if (v != null) { 
v.clear(); 
v = null; 
} 
} 
// 设置事务成功. 
database.setTransactionSuccessful(); 
   
   
return list.size(); 
} catch (Exception e) { 
Logger.error("DAOHelper", "插入失败"); 
return -1; 
} finally { 
database.endTransaction(); 
} 
   
   
} 
   
   
public int delete(String sqlWhere) { 
SQLiteDatabase database = DBOpenHelper.getWritableDatabase(); 
try { 
// 打开数据库 
database.beginTransaction(); 
// 设置事务成功. 
int rowCount = database.delete(tableName, sqlWhere, null); 
database.setTransactionSuccessful(); 
return rowCount; 
} catch (Exception e) { 
Logger.error("DAOHelper", "删除失败"); 
return -1; 
} finally { 
database.endTransaction(); 
} 
} 
   
   
private ContentValues mapToContentValues(Map<String, String> map) { 
   
   
ContentValues values = new ContentValues(); 
for (String col : cols) { 
values.put(col, map.get(col)); 
} 
   
   
return values; 
} 
   
   
static public void clear(List<Map<String, String>> list) { 
if (null == list) { 
return; 
} 
for (Map<String, String> map : list) { 
if (null != map) { 
map.clear(); 
} 
} 
list.clear(); 
} 
   
/** 
* 查询得到列表 
*  
* @param sql 
*             完整的select语句,可包含?,但不能用;结尾 
* @param selectionArgs 
*             查询参数 
* @param rp 
*              每行的处理,可使用DAOHelper.MAPROWPROCESSOR 
* @return 
*/ 
static public <T> List<T> query(String sql, String[] selectionArgs, RowProcessor<T> rp) { 
ArrayList<T> list = new ArrayList<T>(0); 
   
   
Cursor c = null; 
try { 
   
   
SQLiteDatabase database = DBOpenHelper.getWritableDatabase(); 
   
   
c = database.rawQuery(sql, selectionArgs); 
   
list.ensureCapacity(c.getCount()); 
   
   
while (c.moveToNext()) { 
list.add(rp.process(c)); 
} 
} catch (Exception e) { 
e.printStackTrace(); 
Logger.error("DAOHelper", "查询失败//n"+e); 
   
   
} finally { 
   
   
if (null != c) { 
c.close(); 
} 
} 
   
   
return list; 
} 
   
static public int count(String sql, String[] selectionArgs) { 
Cursor c = null; 
try { 
   
SQLiteDatabase database = DBOpenHelper.getWritableDatabase(); 
   
c = database.rawQuery(sql, selectionArgs); 
   
return c.getCount(); 
   
} catch (Exception e) { 
e.printStackTrace(); 
Logger.error("DAOHelper", "查询失败//n"+e); 
   
} finally { 
   
if (null != c) { 
c.close(); 
} 
} 
   
return 0; 
} 
   
//行处理接口 
public interface RowProcessor<T> { 
T process(Cursor c); 
} 
   
   
//将每行处理成Map<String,String>结构 
static public class MapRowProcessor implements RowProcessor<Map<String,String>> { 
   
   
@Override 
public Map<String,String> process(Cursor c) { 
Map<String,String> map = new CaseInsensitiveMap<String>(); 
   
   
String[] columns = c.getColumnNames(); 
   
   
for (String col : columns) { 
map.put(col, c.getString(c.getColumnIndex(col))); 
} 
   
   
return map; 
} 
   
   
} 
   
//将每行处理成String结构 
static public class StringRowProcessor implements RowProcessor<String> { 
private String[] fields; 
private String joinner; 
private volatile int[] fldIdx = null; 
   
public StringRowProcessor(){ 
this.fields = null; 
this.joinner = ","; 
} 
   
public StringRowProcessor(String[] fields,String joinner){ 
this.fields = fields; 
if(null != joinner){ 
this.joinner = joinner; 
} 
} 
   
public StringRowProcessor(int[] fieldIndex,String joinner){ 
fldIdx = fieldIndex; 
if(null != joinner){ 
this.joinner = joinner; 
} 
} 
   
@Override 
public String process(Cursor c) { 
if(null == fldIdx){ 
initFldIdx(c); 
} 
   
StringBuilder builder = new StringBuilder(); 
for(int i = 0,n = fldIdx.length; i < n; i ++){ 
builder.append(c.getString(fldIdx[i])); 
if(i < n -1){ 
builder.append(joinner); 
} 
} 
   
return builder.toString(); 
} 
   
private void initFldIdx(Cursor c) { 
if(null == fields){ 
for(int i = 0,n = c.getColumnCount(); i < n ; i ++ ){ 
fldIdx[i] = i; 
} 
}else{ 
for(int i = 0, n = fields.length; i < n; i ++){ 
fldIdx[i] = c.getColumnIndex(fields[i]); 
} 
} 
} 
   
} 
} 
  

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/10656.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论