android中仿dbutil的sqlite操作工具类

不好用也不会放出来了,如果对sqllite使用jdbc的驱动来操作,那么使用apache的dbutils最好不过了,但要不想引入那么多的类,使用sqllite的原生操作,那么只引入下面两个类也基本可以了.

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
/**
 * sqlite的dao操作帮助类
 * 
 * @author weisir
 * 
 */

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) {
        List<Map<String, String>> list = new LinkedList<Map<String, String>>();

        Cursor cursor = null;
        try {
            SQLiteDatabase database = DBOpenHelper.getWritableDatabase();
            cursor = database.query(tableName, cols, sqlWhere, sqlWhereArgs, null, null, null);

            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) {
        List<T> list = new LinkedList<T>();

        Cursor c = null;
        try {

            SQLiteDatabase database = DBOpenHelper.getWritableDatabase();

            c = database.rawQuery(sql, selectionArgs);

            while (c.moveToNext()) {
                list.add(rp.process(c));
            }
        } catch (Exception e) {
            Logger.error("DAOHelper", "查询失败");

        } finally {

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

        return list;
    }

    //行处理接口
    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;
        }

    }
}

 

转载于:https://www.cnblogs.com/weiwelcome0/archive/2013/05/04/3059285.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值