首先在dependencies闭包添加依赖
compile 'org.litepal.android:core:1.5.1'
在APP目录下创建assets
<litepal>
<dbname value="BookStroe"></dbname>
<version value='1'></version>
<list>
<mapping class="textitei.litepal.Book"></mapping>
</list>
</litepal>
定义要写入数据库中的类
public class Book extends DataSupport{
private int id;
private String Author;
private double price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAuthor() {
return Author;
}
public void setAuthor(String author) {
Author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
在Activity中进行增删查改
case R.id.bt_create:
Connector.getDatabase();
break;
case R.id.bt_insert:
Book book=new Book();
book.setId(1);
book.setAuthor("刘营海");
book.setPrice(90);
book.save();
break;
case R.id.bt_updata:
Book book1=new Book();
book1.setPrice(80);
book1.updateAll("Author=?","刘营海");
break;
case R.id.bt_delete:
DataSupport.deleteAll(Book.class,"Author=?","刘营海");
break;
case R.id.bt_select:
List<Book> books=DataSupport.findAll(Book.class);
for (Book book2:books){
mTvshow.append(book2.toString());