Litepal是一款开源的Android数据库框架,它采用了对象关系映射(ORM)的模式,并将最最常用的数据库功能进行了封装,使得不用编写一行SQL语句就可以完成各种建表和增删改查的操作。
Book.java
package com.example.litepaltest;
import org.litepal.crud.DataSupport;
/**
* Created by Codingbo on 2017/2/2.
*/
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;
}
}
Category.java
package com.example.litepaltest;
/**
* Created by Codingbo on 2017/2/2.
*/
public class Category {
private int id;
private String categoryName;
private int categoryCode;
public void setId(int id){
this.id=id;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public void setCategoryCode(int categoryCode) {
this.categoryCode = categoryCode;
}
}
MainActivity.java
package com.example.litepaltest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import org.litepal.crud.DataSupport;
import org.litepal.tablemanager.Connector;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@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();
}
});
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("The da Vinci code");
book.setAuthor("Dan Brown");
book.setPrice(16.96);
book.setPress("Unknow");
book.save();
}
});
Button updateData =(Button)findViewById(R.id.update_data);
updateData.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.95);
book.setPress("Unknow");
book.save();
book.setPrice(10.99);
book.save(); */
Book book = new Book();
book.setPrice(14.95);
book.setPress("Anchor");
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 books =DataSupport.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());
}
}
});
}
}
litepal.xml
<litepal>
<dbname value="BookStore">dbname>
<version value="2">version>
<list>
<mapping class="com.example.litepaltest.Book">mapping>
<mapping class="com.example.litepaltest.Category">mapping>
list>
litepal>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<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/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"
/>
<Button
android:id="@+id/query_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Query data"
/>
LinearLayout>
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.litepaltest">
<application
android:name="org.litepal.LitePalApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
intent-filter>
activity>
application>
manifest>
查看删除后的数据