接上一篇:

《在Android上使用OrmLite数据库框架 之 基本用法》


说明:OrmLite可能通过实体类加注解的方式实现了对数据库的操作,这种方式非常方便,

不过我们可以让程序跑得更有效率,因为我们不需要让程序每次操作数据库都得解析一下实体的注解。

Android基情群:91488018 (安卓是会呼吸的痛)

----------------------------基情线------------------------------

1.先把Android项目调成可以跑main()方法

第一步不会的,点这里!


2.在res目录下建立raw目录(少了这步无法进行)

3.新建一个工具类,大概如下:

publicclass DatabaseConfigUtil extends OrmLiteConfigUtil { privatestatic final Class<?>[] classes = new Class[] { Department.class,Employee.class}; publicstatic void main(String[] args) throws Exception {writeConfigFile("ormlite_config.txt",classes);}}
好吧,看我写的:

package com.deng.orm;import com.deng.orm.vo.Department;import com.deng.orm.vo.Employee;import com.j256.ormlite.android.apptools.OrmLiteConfigUtil;/** * @author 剑戟 * @version 创建时间:2011-12-16 下午02:24:40 */public class TableUtil extends OrmLiteConfigUtil {private static final Class<?>[] classes = new Class[] { Department.class,Employee.class };public static void main(String[] args) throws Exception {System.out.println("### begin...");writeConfigFile("ormlite_config.txt", classes);System.out.println("### end...");}}


说明:classes数组里把要映射的实体类装入;本类是在开发环境下运行使用,运行后会在raw目录下生成ormlite_config.txt文件,内容大概如下:

## generated on2011/12/16 02:40:22## --table-start--dataClass=com.deng.orm.vo.DepartmenttableName=department#--table-fields-start--# --field-start--fieldName=idgeneratedId=true# --field-end--# --field-start--fieldName=departNameunique=true# --field-end--#--table-fields-end--# --table-end--################################## --table-start--dataClass=com.deng.orm.vo.EmployeetableName=TB_EMP#--table-fields-start--# --field-start--fieldName=empIDgeneratedId=true# --field-end--# --field-start--fieldName=name# --field-end--# --field-start--fieldName=age# --field-end--# --field-start--fieldName=departforeign=trueforeignAutoRefresh=true# --field-end--#--table-fields-end--# --table-end--#################################


此时,R类中自动生成如下:

    public static final class raw {        public static final intormlite_config=0x7f040000;    }


4.在DatabaseHelper中改成如下:

publicDatabaseHelper(Context context) {  super(context, DATABASE_NAME, null,DATABASE_VERSION,    R.raw.ormlite_config);}

让程序用上刚才我们生成的表配置文件

5.程序启动时,看到如下Log表明加载配置文件成功:

12-16 07:20:07.973:I/DaoManager(696): Loaded configuration for class com.deng.orm.vo.Department12-16 07:20:07.973:I/DaoManager(696): Loaded configuration for class com.deng.orm.vo.Employee

Demo高清有码下http://download.csdn.net/detail/oo8_8oo/4096822



更多相关文章

  1. Android创建和使用数据库详… 分类: Android数据存储 ...
  2. Android创建和使用数据库详细指南
  3. Android使用Jdbc连接远程数据库
  4. Android(安卓)Studio 单刷《第一行代码》系列 03 —— Activity
  5. Android中通过WebView控件实现与JavaScript方法相互调用的地图应
  6. android recovery mode
  7. android 使用contentobserver监听数据库内容变化
  8. android使用mysql的方法总结
  9. Android(安卓)注解基本使用

随机推荐

  1. android内核编译方法
  2. [置顶] Android面试题【高级工程师版】
  3. android下不规则多边形填充位图
  4. Android : 隐藏软键盘
  5. Android SplashActivity启动时黑屏的问题
  6. Android包(android.view.animation)的简介
  7. Android: Android Light Sensor HOWTO
  8. android 获取位置
  9. Android Studio之编译t提示Invoke-custom
  10. Android添加USB add-on硬件访问服务