Android持久化技术(二)——使用LitePal

文章均摘自《Android第一行代码》(第二版)

使用LitePal操作数据库

        LitePal是一款开源的Android数据库框架,采用了对象关系映射的模式,并将我们平时开发最常用到的一些数据库功能进行了封装。
使用之前:

        添加依赖:implementation ‘org.litepal.guolindev:core:3.2.3’
        在app/src/main目录下创建assets目录,在该目录下创建litepal.xml文件
        向litepal.xml文件中添加内容

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

添加了Book类和Category类

package com.example.litepaltest66;

import org.litepal.crud.LitePalSupport;

public class Book extends LitePalSupport {
    private int id;
    private String author;
    private double price;
    private int pages;
    private String name;

    private String press;

    public String getPress() {
        return press;
    }

    public void setPress(String press) {
        this.press = press;
    }

    public int getId() {
        return id;
    }

    public String getAuthor() {
        return author;
    }

    public double getPrice() {
        return price;
    }

    public int getPages() {
        return pages;
    }

    public String getName() {
        return name;
    }

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

    public void setAuthor(String author) {
        this.author = author;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    public void setName(String name) {
        this.name = name;
    }
}
package com.example.litepaltest66;

public class Category {
    private int id;
    private String categoryName;

    public int getId() {
        return id;
    }

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

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }
}

然后建立映射关系(com.example.litepaltest66.Book是Book类的完整包名)

<?xml version="1.0" encoding="utf-8"?>
<litepal>
    <dbname value="BookStore"></dbname>
    <version value="2"></version>
    <list>
        <mapping class= "com.example.litepaltest66.Book"></mapping>
        <mapping class= "com.example.litepaltest66.Category"></mapping>
    </list>
</litepal>

更新数据库时版本号+1(<version value="2"></version>)

接下来就是简单的增删改查操作,跟上面的数据库操作很像

Button addDate = (Button) findViewById(R.id.add_date);
        addDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Book book = new Book();
                book.setName("The Da Vinci Code");
                book.setAuthor("Dan Brown");
                book.setPages(454);
                book.setPrice(16.96);
                book.setPress("Unknow");
                book.save();
                Toast.makeText(MainActivity.this,"You click the addDate",Toast.LENGTH_SHORT).show();
            }
        });

Button updateDate = (Button) findViewById(R.id.update_date);
        updateDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Book book = new Book();
                book.setName("The Lost Symbol");
                book.setAuthor("Dan Brown");
                book.setPages(510);
                book.setPrice(19.96);
                book.setPress("Unknow");
                book.save();
                book.setPrice(10.99);
                book.save();
                book.delete();


                Book book = new Book();
                book.setPrice(14.95);
                book.setPress("Anchor");
                book.updateAll("name = ? and author = ?","The Lost Symbol","Dan Brown" );

                Book book = new Book();
                book.setToDefault("pages");//将页数改成0
                book.updateAll();
                Toast.makeText(MainActivity.this,"You click the updateDate",Toast.LENGTH_SHORT).show();
            }
        });

        另外:在将类的变量修改成默认值时,不能直接设置成0或者null,否则修改无效,正确改法是book.setToDefault("pages");  //将页数改成0

Button deleteDate = (Button) findViewById(R.id.delect_date);
        deleteDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LitePal.deleteAll(Book.class,"Price < ?","17");
                Toast.makeText(MainActivity.this,"You click the deleteDate",Toast.LENGTH_SHORT).show();
            }
        });

Button queryDate = (Button) findViewById(R.id.query_date);
        queryDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<Book> books = LitePal.findAll(Book.class);
                for(Book book : books){
                    Log.d("MainActivity", "book name is " + book.getName());
                    Log.d("MainActivity", "book author is " + book.getAuthor());
                    Log.d("MainActivity", "book pages is " + book.getPages());
                    Log.d("MainActivity", "book price is " + book.getPrice());
                    Log.d("MainActivity", "book Press is " + book.getPress());
                }
                Toast.makeText(MainActivity.this,"You click the queryDate",Toast.LENGTH_SHORT).show();
            }
        });

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值