LitePal的使用

今天看了一下关于LitePal的使用,写得很详细,很好,但是看完之后就知道如何使用了,我自己就将我需要的重点摘出来,以后可以随时回顾;

首先要会查看数据库 一般推荐使用可视化工具 网上一艘一堆 有在PC端查看的 也可以在手机上使用Root Explorer直接查看

如果不想使用以上工具也可以用cmd查看  以下是一些能用到的cmd命令;

首先确保模拟器或者是root过的机子连接了电脑

输入adb shell

注意#符号表示我们当前已经是超级用户了,如果显示的是$符号,表示当前只是普通用户而已,这时还需要输入su命令切换一下用户身份才行。

cd 后跟文件夹名会打开目标文件夹,ls 查看该文件夹里所有子目录;

databases肯定是用于存放数据库文件的,files是用于存放普通文本文件的,lib是用于存放so库的,shared_prefs则是用于存放shared文件的。文件格式是xxx.db的文件就是数据库文件;

打开数据库文件sqlite3 xxx.db ; 

查看数据库中所有表格 .table

查看表的数据结构 pragma table_info(TABLE_NAME);

可以通过 .mode line 修改表格显示格式

标注红色的命令注意以分好结束;

SQLite数据库中都还有一个隐藏的sqlite_master表,这里记载了当前数据库中所有表的建表语句,可以使用select * from sqlite_master命令进行查看

准备工作差不多了  开始使用

LitePal的GitHub地址

1、直接使用下载jar包使用或者在studio中添加依赖就行;

2、配置litepal.xml文件

在assets文件夹下创建litepal.xml文件,将下面代码拷贝到里面;

<?xml version="1.0" encoding="utf-8"?>  
<litepal>  
    <dbname value="demo" ></dbname>  
  
    <version value="1" ></version>  
  
    <list>  
    </list>  
</litepal>  

<dbname>用于设定数据库的名字,<version>用于设定数据库的版本号,<list>用于设定所有的映射模型;

3、配置LitePalApplication

<manifest>  
    <application  
        android:name="org.litepal.LitePalApplication"  
        ...  
    >  
    ...  
    </application>  
</manifest>  
有些程序可能会有自己的Application,并在这里配置过了。 没有关系,这时只需要修改一下MyApplication的继承结构,让它不要直接继承Application类,而是继承LitePalApplication类。有些程序可能会遇到一些更加极端的情况,比如说MyApplication需要继承另外一个AnotherApplication,并且这个AnotherApplication还是在jar包当中的,不能修改它的代码。这种情况应该算是比较少见了,但是如果你遇到了的话也不用急,仍然是有解释方案的。你可以把LitePal的源码下载下来,然后把src目录下的所有代码直接拷贝到你项目的src目录下面,接着打开LitePalApplication类,将它的继承结构改成继承自AnotherApplication,再让MyApplication继承自LitePalApplication;


开始建表


public class News {  
      
    private int id;  
      
    private String title;  
      
    private String content;  
      
    private Date publishDate;  
      
    private int commentCount;  
      
    // 自动生成get、set方法  
    ...  
}  
模型类建好后,就可以将它配置到映射列表中了;

<?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>  
配置完只要执行以下代码就会自动建表了;

Connector.getDatabase(); 

如果要修改数据库或者添加删除数据库,将修改的代码设置完毕后 将litepal.xml文件里面的版本加一即可;
以四个表为例 News、Category、Introduction、Comment;
News和Introduction是一对一的关系
public class News {  
    ...  
    private Introduction introduction;  
      
    // 自动生成get、set方法  
}  
Comment和News是多对一的关系
public class News {  
    ...  
    private Introduction introduction;  
      
    private List<Comment> commentList = new ArrayList<Comment>();  
      
    // 自动生成get、set方法  
}  

public class Comment {  
    ...  
    private News news;  
      
    // 自动生成get、set方法   
}  


News和Category是多对多
public class News {  
    ...  
    private Introduction introduction;  
      
    private List<Comment> commentList = new ArrayList<Comment>();  
      
    private List<Category> categoryList = new ArrayList<Category>();  
      
    // 自动生成get、set方法  
}

public class Category {  
    ...  
    private List<News> newsList = new ArrayList<News>();  
      
    // 自动生成get、set方法  
}  
最后添加映射就行了;
<?xml version="1.0" encoding="utf-8"?>  
<litepal>  
    <dbname value="demo" ></dbname>  
  
    <version value="4" ></version>  
  
    <list>  
        <mapping class="com.example.databasetest.model.News"></mapping>  
        <mapping class="com.example.databasetest.model.Comment"></mapping>  
        <mapping class="com.example.databasetest.model.Introduction"></mapping>  
        <mapping class="com.example.databasetest.model.Category"></mapping>  
    </list>  
</litepal>  
存储数据
 让所有的实体类集成自 DataSupport这个类;
public class News extends DataSupport{  
      
    ......  
      
    // 自动生成get、set方法  
} 
News news = new News();  
news.setTitle("这是一条新闻标题");  
news.setContent("这是一条新闻内容");  
news.setPublishDate(new Date());  
news.save(); 
save()方法返回的是个布尔值,也可以用saveThrows(),储存失败会抛出一个异常;
如果是集合的话可以使用;
List<News> newsList;  
...  
DataSupport.saveAll(newsList); 
修改数据
定义的方法为
public static int update(Class<?> modelClass, ContentValues values, long id)  

ContentValues values = new ContentValues();  
values.put("title", "今日iPhone6发布");  
DataSupport.update(News.class, values, 2);  
ContentValues values = new ContentValues();  
values.put("title", "今日iPhone6 Plus发布");  
DataSupport.updateAll(News.class, values, "title = ? and commentcount > ?", "今日iPhone6发布", "0");  


删除数据
public static int delete(Class<?> modelClass, long id)  

public static int deleteAll(Class<?> modelClass, String... conditions)  

查询数据
简单查询
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> newsList = DataSupport.where("commentcount > ?", "0").find(News.class);  
 里面的条件可以根据自己需要添加;
List<News> newsList = DataSupport.select("title", "content")  
        .where("commentcount > ?", "0")  
        .order("publishdate desc").limit(10).offset(10)  
        .find(News.class);  
激进查询
建议修改一下表中的代码;
 
public class News extends DataSupport{  
      
    ...  
  
    public List<Comment> getComments() {  
        return DataSupport.where("news_id = ?", String.valueOf(id)).find(Comment.class);  
    }  
      
}  
 原生查询
 和原生的SQL语句的返回结果是相同的;
Cursor cursor = DataSupport.findBySQL("select * from news where commentcount>?", "0");  

聚合函数

LitePal中一共提供了count()、sum()、average()、max()和min()这五种聚合函数;
 count()统计一共多少行;
int result = DataSupport.count(News.class); 

int result = DataSupport.where("commentcount = ?", "0").count(News.class); 
  sum()求和;
int result = DataSupport.sum(News.class, "commentcount", int.class); 
   average()求平均数;
double result = DataSupport.average(News.class, "commentcount");  
   最大值;
int result = DataSupport.max(News.class, "commentcount", int.class);  
   最小值;
int result = DataSupport.min(News.class, "commentcount", int.class); 



























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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值