android_数据库操作_LitePal框架

LitePal轻量级对象关系映射(ORM),配置简单,下载地址 : https://github.com/LitePalFramework/LitePal

使用步骤
  1. 添加Jar文件

  2. assets文件夹中添加 litepal.xml

?
1
2
3
4
5
6
7
8
9
<?xml version= "1.0"  encoding= "utf-8" ?>
<litepal>
     <dbname value= "demo"  ></dbname>数据库名称
     <version value= "1"  ></version>版本号
     <list>
         <mapping  class = "com.test.model.Reader" ></mapping>对应的实体
         <mapping  class = "com.test.model.Magazine" ></mapping>
     </list>
</litepal>

 3. 配LitePalApplication

没有自定义Application时<application  android:name="org.litepal.LitePalApplication"  />  

自定义Application需要继承另外一个AnotherApplication,并且这个AnotherApplication还是在jar包当中的,不能修改它的代码。可以把LitePal的源码下载下来,然后把src目录下的所有代码直接拷贝到你项目的src目录下面,接着打开LitePalApplication类,将它的继承结构改成继承自AnotherApplication,再让自定义Application继承自LitePalApplication,这样所有的Application就都可以在一起正常工作了。

LitePal建表

建一张news标就要有对应的News实体类,可以进行对象关系映射的数据类型一共有8种,int、short、long、float、double、boolean、String和Date

?
1
2
3
4
5
6
7
8
9
10
public  class  News {
     private  int  id;
     private  String title;
     private  String content;
     private  Date publishDate;
     private  int  commentCount;
     
     // 自动生成get、set方法
     ...
}

编辑assets目录下的litepal.xml文件

?
1
2
3
4
5
6
7
8
<?xml version= "1.0"  encoding= "utf-8" ?>
<litepal>
     <dbname value= "demo"  ></dbname>
     <version value= "1"  ></version>
     <list>
         <mapping  class = "com.example.databasetest.model.News" ></mapping>完整类名
     </list>
</litepal>

获取SQLiteDatabase实例  SQLiteDatabase db = Connector.getDatabase(); 

LitePal升级表

更改litepal.xml中得

<version value="2" ></version> 

<list>   <mapping class="com.example.databasetest.model.News"></mapping>        <mapping class="com.example.databasetest.model.Comment"></mapping>     </list>  

LitePal操作数据

让所有实体对象继承DataSuppert

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
News news =  new  News();
news.setTitle( "这是一条新闻标题" );
news.setContent( "这是一条新闻内容" );
news.setPublishDate( new  Date());
boolean  flag = news.save();
news.saveThrows();  //存储失败会抛异常
存储后会自动给id赋上值news.getId();
 
//存储列表
List<News> newsList;
...
DataSupport.saveAll(newsList);
 
//修改
ContentValues values =  new  ContentValues();
values.put( "title" "今日iPhone6发布" );
DataSupport.update(News. class , values,  2 ); //指定id
 
ContentValues values =  new  ContentValues();
values.put( "title" "今日iPhone6 Plus发布" );
DataSupport.updateAll(News. class , values,  "title = ?" "今日iPhone6发布" );
 
DataSupport.updateAll(News. class , values,  "title = ? and commentcount > ?" "今日iPhone6发布" "0" );  //约束条件
 
DataSupport.updateAll(News. class , values);   //无约束条件
 
News updateNews =  new  News();  
updateNews.setToDefault( "commentCount" );  
updateNews.updateAll(); //news表中所有新闻的评论数清零
 
DataSupport.delete(News. class 2 );   //删除记录
DataSupport.deleteAll(News. class "title = ? and commentcount = ?" "今日iPhone6发布" "0" ); 
//删除记录约束条件
DataSupport.deleteAll(News. class );   //删除全部记录
 
if  (news.isSaved()) {  
     news.delete();  
}
LitePal查询
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
News news = DataSupport.find(News. class 1 );  //指定记录
News firstNews = DataSupport.findFirst(News. class );  第一条记录
News lastNews = DataSupport.findLast(News. class ); 最后一条记录
List<News> newsList = DataSupport.findAll(News. class 1 3 5 7 ); 指定记录成列表
long [] ids =  new  long [] {  1 3 5 7  };
List<News> newsList = DataSupport.findAll(News. class , ids);指定记录成列表
List<News> allNews = DataSupport.findAll(News. class );  
 
List<News> newsList = DataSupport.select( "title" "content" )
         .where( "commentcount > ?" "0" )
         .order( "publishdate desc" ).limit( 10 ).offset( 10 )
         .find(News. class ); //链式查询
         
News news = DataSupport.find(News. class 1 true ); //映射属性查询,比较耗时不推荐
List<Comment> commentList = news.getCommentList();
 
public  class  News  extends  DataSupport{
     public  List<Comment> getComments() {
     return  DataSupport.where( "news_id = ?" , String.valueOf(id)).find(Comment. class );
     }
}
 
//原生查询
Cursor cursor = DataSupport.findBySQL( "select * from news where commentcount>?" "0" );

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值