数据库持久化框架
2016-08-09 13:35
13 查看
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<Entity> findData(Dao<Entity, Integer> Entitydao, int id) throws SQLException{
HashMap<String, Object> EntityMap = new HashMap<String, Object>();
userguideMap.put("id", id);
List<Entity> EntityLists = entityDao.queryForFieldValues(EntityMap);
return EntityLists == null ? null : EntityLists;
}
public void addEntity(Dao<Entity, Integer> 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<Entity, Integer> dataDao = getHelper().getDataDao();
//查找操作调用
List<Entity> simpledataList = dao.findData(dataDao, 1);
//添加操作调用
dao.addEntity(dataDao,"demotest");
} catch (SQLException e) {
e.printStackTrace();
} 阅读更多
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<Entity> findData(Dao<Entity, Integer> Entitydao, int id) throws SQLException{
HashMap<String, Object> EntityMap = new HashMap<String, Object>();
userguideMap.put("id", id);
List<Entity> EntityLists = entityDao.queryForFieldValues(EntityMap);
return EntityLists == null ? null : EntityLists;
}
public void addEntity(Dao<Entity, Integer> 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<Entity, Integer> dataDao = getHelper().getDataDao();
//查找操作调用
List<Entity> simpledataList = dao.findData(dataDao, 1);
//添加操作调用
dao.addEntity(dataDao,"demotest");
} catch (SQLException e) {
e.printStackTrace();
} 阅读更多
相关文章推荐
- android数据库持久化框架
- android数据库持久化框架, ormlite框架,
- android数据库持久化框架, ormlite框架,
- android数据库持久化框架
- SQLObject 1.4.0,Python 数据库持久化框架
- android数据库持久化框架
- (柯昌合)Android Sqlite 持久化框架。类似于hibernate的sqlite框架。不用写SQL语句,操作数据库
- 持久化框架:轻量级的关系型数据库中间件 Sharding-JDBC
- Orm数据库持久化框架
- JDBC数据源(DataSource)数据源技术是Java操作数据库的一个很关键技术,流行的持久化框架都离不开数据源的应用。
- android数据库持久化框架
- android数据库持久化框架
- Django框架学习02--模型实现数据库CRUL
- Android ORM 框架:GreenDao 数据库升级
- VS2013与MySql建立连接;您的项目引用了最新实体框架;但是,找不到数据链接所需的与版本兼容的实体框架数据库 EF6使用Mysql的技巧
- thinkcmf/TP 框架往7牛云上传文件、视频、图片并把打开链接写入数据库
- python Flask框架数据库一对多关系,监听器
- Model,与数据库,框架和开发方式无关
- UAP的持久化框架
- 实体框架 (EF) 入门 => 二、在全新的数据库中使用 Code First