首先添加如下依赖
compile 'org.litepal.android:core:1.6.0'
新建assets文件夹,然后在文件夹中添加litepal.xml文件,如下图所示
litepal.xml文件中,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<litepal>
<dbname value="book"/>数据库名称
<version value="1"/>数据库版本
<list>
<mapping class="com.example.liang.lianxi.Book"/>
</list>
</litepal>
修改androidmanifest中的application的name属性。如下所示:
<application
android:name="org.litepal.LitePalApplication"
...
Book文件中
public class Book extends DataSupport{
private int id;
private String author;
private double price;
private int pages;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Mainactivity文件中
public class MainActivity extends AppCompatActivity {
private Button button;
private Button button1;
private Button button2;
private Button button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Connector.getDatabase();
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button1 = (Button) findViewById(R.id.button1);
button2= (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Book book = new Book();
book.setName("老人与海");
book.setAuthor("海明威");
book.setPages(444);
book.setPrice(20.0);
book.save();
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Book book=new Book();
book.setPrice(30.0);
book.updateAll("name='老人与海'");
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DataSupport.deleteAll(Book.class,"name='老人与海'");
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<Book>list=DataSupport.findAll(Book.class);
for(Book book:list){
Log.i("xinxi",book.getName()+","+book.getAuthor()+","+book.getPages()+","+book.getPrice());
}
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:text="增加数据"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button" />
<Button
android:text="更新数据"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button1" />
<Button
android:text="删除数据"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button2" />
<Button
android:text="查询数据"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button3" />
</LinearLayout>