文章均摘自《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();
}
});