1. ormlite框架
1. 从 http://ormlite.com/releases/下载对应的核心包core及android支持库.然后在项目中加入两个jar包.
2. 存储的数据对象实体
public class Entity{
@DatabaseField(generatedId = true)//自增长的主键
int id;
@DatabaseField//声明string为数据库字段
String string;
public Entity() {
//ormlite中必须要有一个无参的构造函数
}
...
}
ormlite是通过注解方式配置该类的持久化参数,其中常用参数如下:
1.表名:不指定的话表名就是类名.
@DatabaseTable(tableName="dataTableName")
2.字段:这个可以配置的属性有点多.
@DatabaseField
(1)主键:
@DatabaseField(id=true)
(2)列名: 不指定的话就是和变量名一样的
@DatabaseField(columnName="columnName")
(3) 数据类型: 这个一般情况下都不用指定,可以根据java 类获得
@DatabaseField(dataType=DataType.INTEGER)
(4) 默认值:
@DatabaseField(defaultValue="0")
(5)长度:一般用于String型
@DatabaseField(width=13)
(6) 能否为空:默认为True
@DatabaseField(canBeNull=false)
3. 需要数据DataHelper类,来创建及管理数据库. DataHelper类继承OrmLiteSqliteOpenHelper.并在覆盖实现onCreate, onUpgrade, close等方法.
@Override
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource){
try{
Log.e(DataHelper.class.getName(), "开始创建数据库");
TableUtils.createTable(connectionSource, Entity.class);
Log.e(DataHelper.class.getName(), "创建数据库成功");
}catch(SQLException e){
Log.e(DataHelper.class.getName(), "创建数据库失败", e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int arg2, int arg3){
try{
TableUtils.dropTable(connectionSource, Entity.class, true);
onCreate(db, connectionSource);
Log.e(DataHelper.class.getName(), "更新数据库成功");
}catch(SQLException e){
Log.e(DataHelper.class.getName(), "更新数据库失败", e);
}
}
@Override
public void close(){
super.close();
}
4. 需要相应的数据Dao类,即数据访问接口.
public class EntityDao{
public List findData(Dao Entitydao, int id) throws SQLException{
HashMap EntityMap = new HashMap();
userguideMap.put("id", id);
List EntityLists = entityDao.queryForFieldValues(EntityMap);
return EntityLists == null ? null : EntityLists;
}
public void addEntity(Dao entityDao, String string) throws SQLException{
Entity Entity =new Entity(string);
entityDao.create(Entity);
}
}
5. 调用
ormlite有两种使用方法,一种是继承对应的OrmLiteBaseActivity.但像xxxx Activity本身必须继承BaseActivity,这样就必须使用另外一种方法:在activity中添加一个获取helper的方法,还有在onDestroy中添加及时关闭helper的方法。
private DataHelper dataHelper = null;
private DataHelper getHelper() {
if (dataHelper == null) {
dataHelper = OpenHelperManager.getHelper(this, DataHelper.class);
}
return dataHelper;
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (dataHelper != null) {
OpenHelperManager.releaseHelper();
dataHelper = null;
}
}
其中数据库操作实现代码如下:
EntityDao dao = new EntityDao();
try {
Dao dataDao = getHelper().getDataDao();
//查找操作调用
List simpledataList = dao.findData(dataDao, 1);
//添加操作调用
dao.addEntity(dataDao,"demotest");
} catch (SQLException e) {
e.printStackTrace();

更多相关文章

  1. [Android(安卓)Studio导入第三方类库方法] Error:(19, 23) 错误:
  2. Android(安卓)中数据库查询方法query()中的selectionArgs的用法
  3. android --相机使用详解概述
  4. Android开发之InstanceState详解
  5. android对话框大全
  6. android 中RecyclerView 嵌套了 GridView 的用法
  7. android中的dispatchTouchEvent、onInterceptTouchEvent和onTouc
  8. 浅谈Java中Collections.sort对List排序的两种方法
  9. Python list sort方法的具体使用

随机推荐

  1. Android(安卓)InputStream与String,Byte
  2. Android刷新页面
  3. Android简单的Fragment嵌套Fragment(View
  4. Android(安卓)Wear 进阶 2 Creating Wear
  5. 在应用程序使用android google搜索功能
  6. [置顶] android 内存泄露那些事情之Handl
  7. android 搜索时关键字变色
  8. android 设置屏幕亮度
  9. android 百度地图 画轨迹
  10. android junit