前面的一篇文章Android ORM系列之ActiveAndroid 介绍了ActiveAndroid的使用方法,但是ActiveAndroid有一个很明显的缺点,就是所有的实体类必须继承Model类才能进行CRUD,这是典型的php中的ActiveRecord方式的数据库操作。这时候我们会想一下,在Java Web中,hibernate或者mybatis就很好,其实在android中也有这么一个框架,但是这个框架是java的,只不过它支持Android而已,它就是ORMLite。这篇文章不会过度介绍ORMlite本身的一些操作。如果感兴趣的话可以阅读下面的几篇翻译的文章.
- Android数据库ORMlite框架翻译系列(第一章)
- Android数据库ORMlite框架翻译系列(第二章:part 1)
- Android数据库ORMlite框架翻译系列(第二章:part 2)
- Android数据库ORMlite框架翻译系列(第二章:part 3)

然而ormlite提供给我们的还不够使用,我们能不能打造一个类似java web那样的dao层的完全分离,其实ORMLite已经为我们提供了这么一个类,它就是com.j256.ormlite.dao.BaseDaoImpl,但是这个类在我写这个dao前还没发现,后来发现有这个类,简直想撞豆腐,不过呢,这个类也不是那么完美,我们还是自己来实现一遍。

我们写的这个dao,如果你对事务要求有较高的要求,建议将dao层中的事务操作提取到service层中去。一个很简单的例子见这篇文章 (android)初探ormlite实现仿web那样写dao,service。这里我为了方便起见,将事务操作都扔到了dao中,建议还是单独提到service层中。下面贴出整个dao的代码,代码很简单。具体内容见注释把。

在使用前加入依赖

 compile 'com.j256.ormlite:ormlite-android:4.48'

我们首先要提供一个Helper类。就是完成数据库的初始化或者更新。然后提供一个helper的单例。

/** * Helper类,提供单例Helper * User:lizhangqu(513163535@qq.com) * Date:2015-08-26 * Time: 12:04 */public class DatabaseHelper extends OrmLiteSqliteOpenHelper {    private static final String DB_NAME = "sc.db";    //数据库名    private static final int DB_VERSION = 1;    //数据库版本    private static DatabaseHelper instance;    //Helper单例    public DatabaseHelper(Context context) {        super(context, DB_NAME, null, DB_VERSION);    }    @Override    public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {        //创建表        try {            TableUtils.createTable(connectionSource, Student.class);            TableUtils.createTable(connectionSource, Course.class);            TableUtils.createTable(connectionSource, StudentCourse.class);        } catch (SQLException e) {            e.printStackTrace();        }    }    @Override    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {        //更新表        try {            TableUtils.dropTable(connectionSource, StudentCourse.class, true);            TableUtils.dropTable(connectionSource, Student.class, true);            TableUtils.dropTable(connectionSource, Course.class, true);            onCreate(database, connectionSource);        } catch (SQLException e) {            e.printStackTrace();        }    }    /**     * 双重加锁检查     *     * @param context 上下文     * @return 单例     */    public static synchronized DatabaseHelper getInstance(Context context) {        if (instance == null) {            synchronized (DatabaseHelper.class) {                if (instance == null) {                    instance = new DatabaseHelper(context);                }            }        }        return instance;    }}

接下来就是这个通用的dao,一个抽象类,抽象方法是getDao,交给子类实现。其实你也可以直接在本类实现,实际差别不会太大。这个dao的主键是Integer类型的,如果你需要其他类型,请自行修改。

/** * 数据库CRUD操作的Dao,子类继承实现抽象方法,也提供一个简单的泛型实现类 * User:lizhangqu(513163535@qq.com) * Date:2015-08-26 * Time: 12:25 */public abstract class BaseDao<T, Integer> {    protected DatabaseHelper mDatabaseHelper;    //helper    protected Context mContext;    //上下文    public BaseDao(Context context) {        if (context == null) {            throw new IllegalArgumentException("Context can't be null!");            //如果为空,则扔出非法参数异常        }        mContext = context.getApplicationContext();        //避免产生内存泄露,使用getApplicationContext()        mDatabaseHelper = DatabaseHelper.getInstance(mContext);        //获得单例helper    }    /**     * 抽象方法,重写提供Dao,在BaseDaoImpl里提供了简单的泛型实现,传递实体类Class即可     *     * @return Dao类     * @throws SQLException SQLException异常     */    public abstract Dao getDao() throws SQLException;    /**     * 增,带事务操作     *     * @param t 泛型实体类     * @return 影响的行数     * @throws SQLException SQLException异常     */    public int save(T t) throws SQLException {        Dao dao = getDao();        DatabaseConnection databaseConnection = null;        try {            databaseConnection = dao.startThreadConnection();            dao.setAutoCommit(databaseConnection, false);            int save = dao.create(t);            dao.commit(databaseConnection);            return save;        } catch (SQLException e) {            dao.rollBack(databaseConnection);            e.printStackTrace();        } finally {            dao.endThreadConnection(databaseConnection);        }        return 0;    }    /**     * 增或更新,带事务操作     * @param t 泛型实体类     * @return Dao.CreateOrUpdateStatus     * @throws SQLException SQLException异常     */    public Dao.CreateOrUpdateStatus saveOrUpdate(T t) throws SQLException {        Dao dao = getDao();        DatabaseConnection databaseConnection = null;        try {            databaseConnection = dao.startThreadConnection();            dao.setAutoCommit(databaseConnection, false);            Dao.CreateOrUpdateStatus orUpdate = dao.createOrUpdate(t);            dao.commit(databaseConnection);            return orUpdate;        } catch (SQLException e) {            dao.rollBack(databaseConnection);            e.printStackTrace();        } finally {            dao.endThreadConnection(databaseConnection);        }        return null;    }    /**     * 增,带事务操作     * @param t 泛型实体类集合     * @return 影响的行数     * @throws SQLException SQLException异常     */    public int save(List t) throws SQLException {        Dao dao = getDao();        DatabaseConnection databaseConnection = null;        try {            databaseConnection = dao.startThreadConnection();            dao.setAutoCommit(databaseConnection, false);            for (T item : t) {                dao.create(item);            }            dao.commit(databaseConnection);            return t.size();        } catch (SQLException e) {            dao.rollBack(databaseConnection);            e.printStackTrace();        } finally {            dao.endThreadConnection(databaseConnection);        }        return 0;    }    /**     * 删,带事务操作     *     * @param t 泛型实体类     * @return 影响的行数     * @throws SQLException SQLException异常     */    public int delete(T t) throws SQLException {        Dao dao = getDao();        DatabaseConnection databaseConnection = null;        try {            databaseConnection = dao.startThreadConnection();            dao.setAutoCommit(databaseConnection, false);            int delete = dao.delete(t);            dao.commit(databaseConnection);            return delete;        } catch (SQLException e) {            dao.rollBack(databaseConnection);            e.printStackTrace();        } finally {            dao.endThreadConnection(databaseConnection);        }        return 0;    }    /**     * 删,带事务操作     *     * @param list 泛型实体类集合     * @return 影响的行数     * @throws SQLException SQLException异常     */    public int delete(List list) throws SQLException {        Dao dao = getDao();        DatabaseConnection databaseConnection = null;        try {            databaseConnection = dao.startThreadConnection();            dao.setAutoCommit(databaseConnection, false);            int delete = dao.delete(list);            dao.commit(databaseConnection);            return delete;        } catch (SQLException e) {            dao.rollBack(databaseConnection);            e.printStackTrace();        } finally {            dao.endThreadConnection(databaseConnection);        }        return 0;    }    /**     * 删,带事务操作     *     * @param columnNames  列名数组     * @param columnValues 列名对应值数组     * @return 影响的行数     * @throws SQLException              SQLException异常     * @throws InvalidParameterException InvalidParameterException异常     */    public int delete(String[] columnNames, Object[] columnValues) throws SQLException, InvalidParameterException {        List list = query(columnNames, columnValues);        if (null != list && !list.isEmpty()) {            Dao dao = getDao();            DatabaseConnection databaseConnection = null;            try {                databaseConnection = dao.startThreadConnection();                dao.setAutoCommit(databaseConnection, false);                int delete = dao.delete(list);                dao.commit(databaseConnection);                return delete;            } catch (SQLException e) {                dao.rollBack(databaseConnection);                e.printStackTrace();            } finally {                dao.endThreadConnection(databaseConnection);            }        }        return 0;    }    /**     * 删,带事务操作     *     * @param id id值     * @return 影响的行数     * @throws SQLException SQLException异常     */    public int deleteById(Integer id) throws SQLException {        Dao dao = getDao();        DatabaseConnection databaseConnection = null;        try {            databaseConnection = dao.startThreadConnection();            dao.setAutoCommit(databaseConnection, false);            int delete = dao.deleteById(id);            dao.commit(databaseConnection);            return delete;        } catch (SQLException e) {            dao.rollBack(databaseConnection);            e.printStackTrace();        } finally {            dao.endThreadConnection(databaseConnection);        }        return 0;    }    /**     * 删,带事务操作     * @param ids id集合     * @return 影响的行数     * @throws SQLException SQLException异常     */    public int deleteByIds(List ids) throws SQLException {        Dao dao = getDao();        DatabaseConnection databaseConnection = null;        try {            databaseConnection = dao.startThreadConnection();            dao.setAutoCommit(databaseConnection, false);            int delete = dao.deleteIds(ids);            dao.commit(databaseConnection);            return delete;        } catch (SQLException e) {            dao.rollBack(databaseConnection);            e.printStackTrace();        } finally {            dao.endThreadConnection(databaseConnection);        }        return 0;    }    /**     * 删,带事务操作     *     * @param preparedDelete PreparedDelete类     * @return 影响的行数     * @throws SQLException SQLException异常     */    public int delete(PreparedDelete preparedDelete) throws SQLException {        Dao dao = getDao();        DatabaseConnection databaseConnection = null;        try {            databaseConnection = dao.startThreadConnection();            dao.setAutoCommit(databaseConnection, false);            int delete = dao.delete(preparedDelete);            dao.commit(databaseConnection);            return delete;        } catch (SQLException e) {            dao.rollBack(databaseConnection);            e.printStackTrace();        } finally {            dao.endThreadConnection(databaseConnection);        }        return 0;    }    /**     * 改,带事务操作     *     * @param t 泛型实体类     * @return 影响的行数     * @throws SQLException SQLException异常     */    public int update(T t) throws SQLException {        Dao dao = getDao();        DatabaseConnection databaseConnection = null;        try {            databaseConnection = dao.startThreadConnection();            dao.setAutoCommit(databaseConnection, false);            int update = dao.update(t);            dao.commit(databaseConnection);            return update;        } catch (SQLException e) {            dao.rollBack(databaseConnection);            e.printStackTrace();        } finally {            dao.endThreadConnection(databaseConnection);        }        return 0;    }    /**     * 改,带事务操作     * @param preparedUpdate PreparedUpdate对象     * @return 影响的行数     * @throws SQLException SQLException异常     */    public int update(PreparedUpdate preparedUpdate) throws SQLException {        Dao dao = getDao();        DatabaseConnection databaseConnection = null;        try {            databaseConnection = dao.startThreadConnection();            dao.setAutoCommit(databaseConnection, false);            int update = dao.update(preparedUpdate);            dao.commit(databaseConnection);            return update;        } catch (SQLException e) {            dao.rollBack(databaseConnection);            e.printStackTrace();        } finally {            dao.endThreadConnection(databaseConnection);        }        return 0;    }    /**     * 查,带事务操作     *     * @return 查询结果集合     * @throws SQLException SQLException异常     */    public List queryAll() throws SQLException {        Dao dao = getDao();        DatabaseConnection databaseConnection = null;        try {            databaseConnection = dao.startThreadConnection();            dao.setAutoCommit(databaseConnection, false);            List query = dao.queryForAll();            dao.commit(databaseConnection);            return query;        } catch (SQLException e) {            dao.rollBack(databaseConnection);            e.printStackTrace();        } finally {            dao.endThreadConnection(databaseConnection);        }        return null;    }    /**     * 查,带事务操作     *     * @param preparedQuery PreparedQuery对象     * @return 查询结果集合     * @throws SQLException SQLException异常     */    public List query(PreparedQuery preparedQuery) throws SQLException {        Dao dao = getDao();        DatabaseConnection databaseConnection = null;        try {            databaseConnection = dao.startThreadConnection();            dao.setAutoCommit(databaseConnection, false);            List query = dao.query(preparedQuery);            dao.commit(databaseConnection);            return query;        } catch (SQLException e) {            dao.rollBack(databaseConnection);            e.printStackTrace();        } finally {            dao.endThreadConnection(databaseConnection);        }        return null;    }    /**     * 查,带事务操作     *     * @param columnName  列名     * @param columnValue 列名对应值     * @return 查询结果集合     * @throws SQLException SQLException异常     */    public List query(String columnName, String columnValue) throws SQLException {        QueryBuilder queryBuilder = getDao().queryBuilder();        queryBuilder.where().eq(columnName, columnValue);        PreparedQuery preparedQuery = queryBuilder.prepare();        Dao dao = getDao();        DatabaseConnection databaseConnection = null;        try {            databaseConnection = dao.startThreadConnection();            dao.setAutoCommit(databaseConnection, false);            List query = dao.query(preparedQuery);            //also can use dao.queryForEq(columnName,columnValue);            dao.commit(databaseConnection);            return query;        } catch (SQLException e) {            dao.rollBack(databaseConnection);            e.printStackTrace();        } finally {            dao.endThreadConnection(databaseConnection);        }        return null;    }    /**     * 查,带事务操作     *     * @param columnNames     * @param columnValues     * @return 查询结果集合     * @throws SQLException SQLException异常     */    public List query(String[] columnNames, Object[] columnValues) throws SQLException {        if (columnNames.length != columnNames.length) {            throw new InvalidParameterException("params size is not equal");        }        QueryBuilder queryBuilder = getDao().queryBuilder();        Where wheres = queryBuilder.where();        for (int i = 0; i < columnNames.length; i++) {            if (i==0){                wheres.eq(columnNames[i], columnValues[i]);            }else{                wheres.and().eq(columnNames[i], columnValues[i]);            }        }        PreparedQuery preparedQuery = queryBuilder.prepare();        Dao dao = getDao();        DatabaseConnection databaseConnection = null;        try {            databaseConnection = dao.startThreadConnection();            dao.setAutoCommit(databaseConnection, false);            List query = dao.query(preparedQuery);            dao.commit(databaseConnection);            return query;        } catch (SQLException e) {            dao.rollBack(databaseConnection);            e.printStackTrace();        } finally {            dao.endThreadConnection(databaseConnection);        }        return null;    }    /**     * 查,带事务操作     *     * @param map 列名与值组成的map     * @return 查询结果集合     * @throws SQLException SQLException异常     */    public List query(Map map) throws SQLException {        QueryBuilder queryBuilder = getDao().queryBuilder();        if (!map.isEmpty()) {            Where wheres = queryBuilder.where();            Iterator> iterator = map.entrySet().iterator();            String key = null;            Object value = null;            for (int i = 0; iterator.hasNext(); i++) {                Map.Entry next = iterator.next();                key = next.getKey();                value = next.getValue();                if (i == 0) {                    wheres.eq(key, value);                } else {                    wheres.and().eq(key, value);                }            }        }        PreparedQuery preparedQuery = queryBuilder.prepare();        Dao dao = getDao();        DatabaseConnection databaseConnection = null;        try {            databaseConnection = dao.startThreadConnection();            dao.setAutoCommit(databaseConnection, false);            List query = dao.query(preparedQuery);            dao.commit(databaseConnection);            return query;        } catch (SQLException e) {            dao.rollBack(databaseConnection);            e.printStackTrace();        } finally {            dao.endThreadConnection(databaseConnection);        }        return null;    }    /**     * 查,带事务操作     *     * @param id id值     * @return 查询结果集合     * @throws SQLException SQLException异常     */    public T queryById(Integer id) throws SQLException {        Dao dao = getDao();        DatabaseConnection databaseConnection = null;        try {            databaseConnection = dao.startThreadConnection();            dao.setAutoCommit(databaseConnection, false);            T t = dao.queryForId(id);            dao.commit(databaseConnection);            return t;        } catch (SQLException e) {            dao.rollBack(databaseConnection);            e.printStackTrace();        } finally {            dao.endThreadConnection(databaseConnection);        }        return null;    }    /**     * 判断表是否存在     *     * @return 表是否存在     * @throws SQLException SQLException异常     */    public boolean isTableExists() throws SQLException {        return getDao().isTableExists();    }    /**     * 获得记录数     *     * @return 记录数     * @throws SQLException SQLException异常     */    public long count() throws SQLException {        Dao dao = getDao();        DatabaseConnection databaseConnection = null;        try {            databaseConnection = dao.startThreadConnection();            dao.setAutoCommit(databaseConnection, false);            long count = dao.countOf();            dao.commit(databaseConnection);            return count;        } catch (SQLException e) {            dao.rollBack(databaseConnection);            e.printStackTrace();        } finally {            dao.endThreadConnection(databaseConnection);        }        return 0;    }    /**     * 获得记录数     *     * @param preparedQuery PreparedQuery类     * @return 记录数     * @throws SQLException SQLException异常     */    public long count(PreparedQuery preparedQuery) throws SQLException {        Dao dao = getDao();        DatabaseConnection databaseConnection = null;        try {            databaseConnection = dao.startThreadConnection();            dao.setAutoCommit(databaseConnection, false);            long count = dao.countOf(preparedQuery);            dao.commit(databaseConnection);            return count;        } catch (SQLException e) {            dao.rollBack(databaseConnection);            e.printStackTrace();        } finally {            dao.endThreadConnection(databaseConnection);        }        return 0;    }}

一个简单的泛型实现类,带缓存。

/** * BaseDao泛型实现类 * User:lizhangqu(513163535@qq.com) * Date:2015-08-26 * Time: 13:51 */public class BaseDaoImpl<T,Integer> extends BaseDao<T,Integer> {    private Class clazz;    private Map,Dao> mDaoMap=new HashMap,Dao>();    //缓存泛型Dao    public BaseDaoImpl(Context context,Class clazz) {        super(context);        this.clazz=clazz;    }    @Override    public Dao getDao() throws SQLException {        Dao dao=mDaoMap.get(clazz);        if (null==dao){            dao=mDatabaseHelper.getDao(clazz);            mDaoMap.put(clazz,dao);        }        return dao;    }}

如果你要增加一些操作,直接继承BaseDao实现抽象方法再加入你要增加的操作,或者直接继承BaseDaoImpl再增加操作即可。

整体代码没有什么难度,而怎么使用呢,这里写了几个测试用例。测试用例中涉及到的实体类就不贴了。

public class ApplicationTest extends ApplicationTestCase {    private BaseDao mStudentDao;    private BaseDao mCourseDao;    private BaseDao mStudentCourseDao;    public ApplicationTest() {        super(Application.class);    }    @Override    protected void setUp() throws Exception {        super.setUp();        mStudentDao=new BaseDaoImpl<>(getContext(),Student.class);        mCourseDao=new BaseDaoImpl<>(getContext(),Course.class);        mStudentCourseDao=new BaseDaoImpl<>(getContext(),StudentCourse.class);    }    public void testAddStudent() throws SQLException {        Student student=new Student();        student.setStudentName("张三");        student.setAge(18);        student.setSex(true);        student.setAddress("杭州市");        mStudentDao.save(student);    }    public void testAddOrUpdateStudent() throws SQLException {        Student student=new Student();        student.setStudentName("张三1111");        student.setAge(18);        student.setSex(true);        student.setAddress("杭州市");        Dao.CreateOrUpdateStatus createOrUpdateStatus = mStudentDao.saveOrUpdate(student);        Log.e("TAG", createOrUpdateStatus.isCreated() + "  " + createOrUpdateStatus.isUpdated() + " " + createOrUpdateStatus.getNumLinesChanged());        student.setAge(19);        createOrUpdateStatus = mStudentDao.saveOrUpdate(student);        Log.e("TAG", createOrUpdateStatus.isCreated() + "  " + createOrUpdateStatus.isUpdated() + " " + createOrUpdateStatus.getNumLinesChanged());    }    public void testAddStudents() throws SQLException {        Student student=new Student();        student.setStudentName("李四");        student.setAge(19);        student.setSex(false);        student.setAddress("温州市");        Student student1=new Student();        student1.setStudentName("王五");        student1.setAge(21);        student1.setSex(true);        student1.setAddress("嘉兴市");        List list=new ArrayList();        list.add(student);        list.add(student1);        mStudentDao.save(list);    }    public void testDeleteStudent() throws SQLException {        Student student=new Student();        student.setStudentName("1111");        student.setAge(11);        student.setSex(true);        student.setAddress("22222");        mStudentDao.save(student);        mStudentDao.delete(student);    }    public void testDeleteStudents() throws SQLException {        Student student=new Student();        student.setStudentName("李四");        student.setAge(19);        student.setSex(false);        student.setAddress("温州市");        Student student1=new Student();        student1.setStudentName("王五");        student1.setAge(21);        student1.setSex(true);        student1.setAddress("嘉兴市");        List list=new ArrayList();        list.add(student);        list.add(student1);        mStudentDao.save(list);        mStudentDao.delete(list);    }    public void testDeleteStudentByColumns() throws SQLException {        Student student=new Student();        student.setStudentName("李四11");        student.setAge(19);        student.setSex(false);        student.setAddress("温州市");        mStudentDao.save(student);        mStudentDao.delete(new String[]{"studentName","studentAge"},new String[]{"李四11","19"});    }    public void testDeleteStudentById() throws SQLException {        Student student=new Student();        student.setStudentName("李四22");        student.setAge(19);        student.setSex(false);        student.setAddress("温州市");        mStudentDao.save(student);        mStudentDao.deleteById(student.getId());    }    public void testDeleteStudentByIds() throws SQLException {        Student student=new Student();        student.setStudentName("李四22");        student.setAge(19);        student.setSex(false);        student.setAddress("温州市");        mStudentDao.save(student);        Student student1=new Student();        student1.setStudentName("李四333");        student1.setAge(19);        student1.setSex(false);        student1.setAddress("温州市");        List list=new ArrayList();        mStudentDao.save(list);        mStudentDao.deleteByIds(Arrays.asList(student.getId(),student1.getId()));    }    public void testDeleteStudentByPreparedDelete() throws SQLException {        Student student=new Student();        student.setStudentName("李四33");        student.setAge(19);        student.setSex(false);        student.setAddress("温州市");        mStudentDao.save(student);        DeleteBuilder studentIntegerDeleteBuilder = mStudentDao.getDao().deleteBuilder();        studentIntegerDeleteBuilder.where().eq("studentName","李四33").and().eq("studentAddress", "温州市");        PreparedDelete prepare = studentIntegerDeleteBuilder.prepare();        mStudentDao.delete(prepare);    }    public void testUpdateStudent() throws SQLException {        Student student=new Student();        student.setStudentName("赵六");        student.setAge(19);        student.setSex(false);        student.setAddress("温州市");        mStudentDao.save(student);        student.setAddress("临安市");        mStudentDao.update(student);    }    public void testUpdateStudentByPreparedUpdate() throws SQLException {        Student student=new Student();        student.setStudentName("赵六");        student.setAge(19);        student.setSex(false);        student.setAddress("温州市");        mStudentDao.save(student);        student.setAddress("临安市");        UpdateBuilder studentIntegerUpdateBuilder = mStudentDao.getDao().updateBuilder();        studentIntegerUpdateBuilder.updateColumnValue("studentName","赵六六").where().eq("studentName", "赵六").and().eq("studentAddress", "温州市");        PreparedUpdate prepare = studentIntegerUpdateBuilder.prepare();        mStudentDao.update(prepare);    }    public void testQueryStudentAll() throws SQLException {        List students = mStudentDao.queryAll();        Log.e("TAG", students + "");    }    public void testQueryStudentByPreparedQuery() throws SQLException {        QueryBuilder studentIntegerQueryBuilder = mStudentDao.getDao().queryBuilder();        studentIntegerQueryBuilder.where().eq("studentAddress","温州市");        PreparedQuery prepare = studentIntegerQueryBuilder.prepare();        List students = mStudentDao.query(prepare);        Log.e("TAG",students+"");    }    public void testQueryStudentByColumn() throws SQLException {        List students = mStudentDao.query("studentName","赵六六");        Log.e("TAG",students+"");    }    public void testQueryStudentByColumns() throws SQLException {        List students = mStudentDao.query(new String[]{"studentName","studentAge","studentSex"},new Object[]{"李四",19,false});        Log.e("TAG",students+"");    }    public void testQueryStudentByMap() throws SQLException {        Map map=new HashMap();        map.put("studentName","李四");        map.put("studentAge",19);        map.put("studentSex",false);        List students = mStudentDao.query(map);        Log.e("TAG",students+"");    }    public void testQueryStudentById() throws SQLException {        Student student = mStudentDao.queryById(1);        Log.e("TAG",student+"");    }    public void testIsTableExists() throws SQLException {        Log.e("TAG",mStudentDao.isTableExists()+"");    }    public void testCount() throws SQLException {        Log.e("TAG",mStudentDao.count()+"");    }    public void testCountByPreparedQuery() throws SQLException {        PreparedQuery prepare = mStudentDao.getDao().queryBuilder().setCountOf(true).where().eq("studentAddress", "温州市").prepare();        Log.e("TAG",mStudentDao.count(prepare)+"");    }

最后给出源码下载地址

  • http://download.csdn.net/detail/sbsujjbcy/9053725

更多相关文章

  1. Android操作HTTP实现与服务器通信
  2. Android文件操作中的openFileOutPut和openFileInput
  3. Android:SNS客户端开发三:数据库操作(一)
  4. 智能电视使用什么操作系统?
  5. Android数据库事务浅析
  6. Android SDCard操作(文件读写,容量计算)
  7. [android]android自动化测试十三之JavaMonkey跨APP操作
  8. [Android] Android 使用 Greendao 操作 db sqlite(1)-- 直接在Ma
  9. Android文件系统的结构及目录用途、操作方法 整理

随机推荐

  1. Android(安卓)从缓存中读取图片并异步加
  2. Android利用Fiddler进行网络数据抓包
  3. Android存储访问及目录
  4. android 从驱动到应用(一)
  5. View Android Source Code in Eclipse
  6. Activity启动模式设置(堆栈中的生存时间)
  7. Android学习目录
  8. Android 学习成品
  9. android点击文本框之外的地方隐藏键盘
  10. Android设置振铃