使用前需要在app/build.gradle文件中添加如下内容:
compile 'org.litepal.android:core:1.4.1'
右键app/src/main新建assets目录,右键该目录,新建litepal.xml:
<?xml version="1.0" encoding="utf-8" ?>
<litepal>
<dbname value="BookStore"></dbname>
<version value="1"></version>
<list>
</list>
</litepal>
在AndroidMainfest中的<application>标签添加下面属性:
android:name="org.litepal.LitePalApplication"
<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>
创建Book类:
public class Book {
private int id;
private String author;
private double price;
private int pages;
private String name;
public String getAuthor() {
return author;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getPages() {
return pages;
}
public double getPrice() {
return price;
}
public void setAuthor(String author) {
this.author = author;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setPages(int pages) {
this.pages = pages;
}
public void setPrice(double price) {
this.price = price;
}
}
然后在litepal.xml中增加一行代码,使其映射到Book类:
<?xml version="1.0" encoding="utf-8" ?>
<litepal>
<dbname value="BookStore"></dbname>
<version value="1"></version>
<list>
<mapping class="top.xiexiaodong.litepaldemo.Book"></mapping>
</list>
</litepal>
之后在映射类,在<list>标签中继续加上就行。
<?xml version="1.0" encoding="utf-8" ?>
<litepal>
<dbname value="BookStore"></dbname>
<version value="1"></version>
<list>
<mapping class="top.xiexiaodong.litepaldemo.Book"></mapping>
<mapping class="top.xiexiaodong.litepaldemo.Category"></mapping>
</list>
</litepal>
它不用先drop在创建,会保存之前的数据。
简单的增删改查:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button createButton = (Button) findViewById(R.id.create_button);
createButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LitePal.getDatabase();
}
});
Button insertButton = (Button) findViewById(R.id.insert_button);
insertButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Book book = new Book();
book.setName("name 1");
book.setAuthor("zh2的");
book.setPages(133);
book.setPrice(123.3);
book.setPress("Unkonw");
book.save();
}
});
// 不能对已经存在的数据进行修改
Button updateButton = (Button) findViewById(R.id.update_button);
updateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Book book = new Book();
book.setName("name 2");
book.setAuthor("zh3的");
book.setPages(143);
book.setPrice(143.3);
book.setPress("Unkonw 2");
book.save();
book.setPrice(11.11);
book.save();
}
});
// 不能设置字段的新值为默认值,int=0等,可以通过book.setToDefaut("pages");方法将pages字段设置成默认值
Button update2Button = (Button) findViewById(R.id.update2_button);
update2Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Book book = new Book();
book.setPrice(324.1);
book.setPress("Unkonw 3");
book.updateAll("name = ? and author = ?", "name 2", "zh3的");
}
});
Button deleteButton = (Button) findViewById(R.id.delete_button);
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DataSupport.deleteAll(Book.class, "price < ?", "15");
}
});
Button selectButton = (Button) findViewById(R.id.select_button);
selectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<Book> books = DataSupport.findAll(Book.class);
for (Book book : books) {
Log.d("xxd", book.getName() + book.getAuthor() + book.getPages() + book.getPrice() + book.getPress());
}
}
});
}
}
查询第一条数据:
Book firstBook = DataSupport.findFirst(Book.class);
查询最后一条语句:
Book lastBook = DataSupport.findLast(Book.class);
-
select()方法用于指定查询那几列数据。
List<Book> books = DataSupport.select("name", "author").find(Book.class);
-
where()方法用于指定查询的约束条件。
List<Book> books = DataSupport.where("pages > ?", "300").find(Book.class);
-
order()方法用于指定结果的排列方式。
List<Book> books = DataSupport.order("price desc").find(Book.class);
-
limit()方法用于指定查询结果的数量。
List<Book> books = DataSupport.limit(3).find(Book.class);
-
offset()方法用于指定查询结果的偏移量。
List<Book> books = DataSupport.limit(3).offset(1).find(Book.class);
- limit(3)表示前3个数据,offset(1)偏移1后结果变成第2,3,4个数据了。
以上方法任意组合。如果特殊需求,可用原生SQL:
Cursor c = DataSupport.findBySQL("select * from Book where pages > ? and price < ?", "300", "20");
findBySQL()原生查询。