//布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hzx.litepal.MainActivity">
<Button
android:id="@+id/create_database"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Create database"/>
<Button
android:id="@+id/add_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add data"/>
<Button
android:id="@+id/query_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Query data"/>
<Button
android:id="@+id/update_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Update data"/>
<Button
android:id="@+id/delete_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Delete data"/>
</LinearLayout>
//保存数据的类
public class Book extends DataSupport{
private int id;
private String author;
private double price;
private int pages;
private String name;
private String press;
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;
}
public String getPress(){
return press;
}
public void setPress(String press){
this.press = press;
}
}
//测试类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建
Button createDatabase = (Button)findViewById(R.id.create_database);
createDatabase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Connector.getDatabase();
Book book = new Book();
book.setName("The Lost Symbol");
book.setAuthor("Dan BROWN");
book.setPages(454);
book.setPrice(16.96);
book.setPress("Unknow");
book.save();
}
});
//添加
Button addData = (Button)findViewById(R.id.add_data);
addData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Book book = new Book();
book.setName("hong lou meng");
book.setAuthor("luo guan zhong");
book.setPages(454);
book.setPrice(10);
book.setPress("Unknow");
book.save();
}
});
//更新
Button updataData = (Button)findViewById(R.id.update_data);
updataData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/*
Book book = new Book();
book.setName("xi you ji");
book.setAuthor("luo guan zhong");
book.setPages(510);
book.setPrice(19.95);
book.setPress("Unkonw");
book.save();
book.setPrice(10.99);
book.save();
*/
Book book = new Book();
book.setPrice(1);
book.setPress("AA");
book.updateAll("name = ? and author = ?","The Lost Symbol","Dan BROWN");
}
});
//删除
Button deleteButton = (Button)findViewById(R.id.delete_data);
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DataSupport.deleteAll(Book.class,"price < ?","15");
}
});
//查询
Button queryButton = (Button)findViewById(R.id.query_data);
queryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
List<Book> books = DataSupport.findAll(Book.class);
for (Book book: books){
Log.d("123","name is "+ book.getName());
Log.d("123","author is "+ book.getAuthor());
Log.d("123","Pages is "+ book.getPages());
Log.d("123","Price is "+ book.getPrice());
Log.d("123","Press is "+ book.getPress());
}
}
});
//end
}
网友评论