【Android开发】【数据库】LitePal 数据库的使用

一,导包

dependencies {
    ......
    // LitePal的包
    compile 'org.litepal.android:core:1.3.1'
    ......
}

二,创建bean类

public class Person extends DataSupport {

    private int id;
    private String name;
    private int age;

    public Person(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

三,配置litepal.xml文件

在assets文件夹下创建litepal.xml文件。

<?xml version="1.0" encoding="utf-8"?>
<litepal>
    <dbname value="demo"></dbname>
    <version value="1"></version>
    <list>
        <mapping class="com.example.administrator.litepaldemo.bean.Person"></mapping>
    </list>
</litepal>

“dbname ” 标签, 是数据库的名称,自定义。
“mapping” 标签,填写的是需要进行数据库操作的bean类,要写完整的类名。
如果修改bean类的数据结构,只需要修改此处的数据库版本号即可。

四,配置Application类

让自己的Application继承自LitePalApplication;
如果无法实现可以不继承,但是要在Application中添加:

//初始化LitePal数据库
LitePalApplication.initialize(this);

五,Insert

1, 插入一条数据

 Person person = new Person(4, "Cery", 18);
 person.save(); // 返回值:是否插入成功
  1. 同时插入多条
List<News> newsList;  
...  
DataSupport.saveAll(newsList);

六,Delete

1, 清空数据库

DataSupport.deleteAll(News.class);  

2,根据ID删除某个一条

DataSupport.delete(News.class, 2);

3, 通过where语句来批量删除数据

DataSupport.deleteAll(News.class, "title = ? and commentcount = ?", "今日iPhone6发布", "0");  

七,Update

1, 把news表中id为2的记录的标题改成“今日iPhone6发布”

ContentValues values = new ContentValues();  
values.put("title", "今日iPhone6发布");  
DataSupport.update(News.class, values, 2);  

2,把news表中标题为“今日iPhone6发布”的所有新闻的标题改成“今日iPhone6 Plus发布”

ContentValues values = new ContentValues();  
values.put("title", "今日iPhone6 Plus发布");  
DataSupport.updateAll(News.class, values, "title = ?", "今日iPhone6发布");  

3,把news表中所有新闻的标题都改成“今日iPhone6发布”

ContentValues values = new ContentValues();  
values.put("title", "今日iPhone6 Plus发布");  
DataSupport.updateAll(News.class, values); 

八,Search

//查询第一个
News firstNews = DataSupport.findFirst(News.class);  
//查询最后一个
News lastNews = DataSupport.findLast(News.class);  
//指定多个id来查询
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.where("commentcount > ?", "0").find(News.class);  
//只要title和content这两列数据的条件查询
List<News> newsList = DataSupport.select("title", "content") .where("commentcount > ?", "0").find(News.class); 
//倒叙
List<News> newsList = DataSupport .where("commentcount > ?", "0") .order("publishdate desc").find(News.class);  
//只要前十条数据
List<News> newsList = DataSupport .where("commentcount > ?", "0").limit(10).find(News.class);  
//从第五个往后的十条,不包括第五条
List<News> newsList = DataSupport.limit(10).offset(5).find(News.class);  

查询语句繁杂, 可以参考:http://blog.csdn.net/guolin_blog/article/details/40153833

九,Demo地址

https://github.com/baitutang1221/LitePalDemo

十,参考文章

http://blog.csdn.net/guolin_blog/article/details/38556989
http://blog.csdn.net/guolin_blog/article/details/39345833
http://blog.csdn.net/guolin_blog/article/details/40083685
http://blog.csdn.net/guolin_blog/article/details/40153833
http://blog.csdn.net/u012453607/article/details/51280290

posted @ 2018-12-27 15:01  浪里小白龙呼呼呼  阅读(582)  评论(0编辑  收藏  举报