簡介
LitePal是一款開源的Android資料庫框架,採用了關係對映的模式。LitePal在github上的專案地址:github.com/LitePalFram…
配置LitePal
dependencies {
....
compile 'org.litepal.android:core:1.5.1'
}複製程式碼
在app/src/main 目錄下新建目錄assets,在assets目錄下建litepal.xml 檔案
<?xml version="1.0" encoding="utf-8"?>
<litepal>
<dbname value="Person" ></dbname>
<version value="1" ></version>
<list>
<mapping class="com.zhoujian.litepal.bean.Person"></mapping>
</list>
</litepal>複製程式碼
標籤用於指定所有的對映模型
在清單檔案中配置LitePalApplication
<application
android:name="org.litepal.LitePalApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>複製程式碼
實戰
建立JavaBean
Person.java
package com.zhoujian.litepal.bean;
import org.litepal.crud.DataSupport;
/**
* Created by zhoujian on 2017/3/29.
*/
public class Person extends DataSupport
{
private String name;
private int age;
private int id;
private String weight;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}複製程式碼
增刪改查資料庫
MainActivity.java
package com.zhoujian.litepal.activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.zhoujian.litepal.R;
import com.zhoujian.litepal.bean.Person;
import org.litepal.crud.DataSupport;
import org.litepal.tablemanager.Connector;
import java.util.List;
public class MainActivity extends AppCompatActivity
{
private Button mCreateDatabase;
private Button mAddData;
private Button mUpdateData;
private Button mDeleteButton;
private Button mQueryButton;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
clickEvents();
}
private void initViews()
{
mCreateDatabase = (Button) findViewById(R.id.create_database);
mAddData = (Button) findViewById(R.id.add_data);
mUpdateData = (Button) findViewById(R.id.update_data);
mDeleteButton = (Button) findViewById(R.id.delete_data);
mQueryButton = (Button) findViewById(R.id.query_data);
}
private void clickEvents()
{
mCreateDatabase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//建立資料庫
Connector.getDatabase();
}
});
mAddData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Person person = new Person();
person.setId(1);
person.setName("周潤發");
person.setAge(62);
person.setWeight("80kg");
person.save();
person.clearSavedState();
person.setId(2);
person.setName("周杰倫");
person.setAge(45);
person.setWeight("65kg");
person.save();
person.clearSavedState();
person.setId(3);
person.setName("周星馳");
person.setAge(65);
person.setWeight("70kg");
person.save();
Toast.makeText(MainActivity.this, "新增資料成功", Toast.LENGTH_SHORT).show();
}
});
mUpdateData.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
Person person = new Person();
//把id = 3 的那個人 姓名更改為周建 年齡改為28 體重改為62kg
person.setName("周建");
person.setAge(28);
person.setWeight("62kg");
person.updateAll("id = ?", "3");
Toast.makeText(MainActivity.this, "更新資料成功", Toast.LENGTH_SHORT).show();
}
});
mDeleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
//刪除年齡大於60的人
DataSupport.deleteAll(Person.class, "age > ?", "60");
Toast.makeText(MainActivity.this, "刪除資料成功", Toast.LENGTH_SHORT).show();
}
});
mQueryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
List<Person> persons = DataSupport.findAll(Person.class);
for (Person person: persons) {
Log.d("MainActivity", "person name is " + person.getName());
Log.d("MainActivity", "person weight is " + person.getWeight());
Log.d("MainActivity", "person age is " + person.getAge());
Log.d("MainActivity", "person id is " + person.getId());
}
Toast.makeText(MainActivity.this, "查詢資料成功", Toast.LENGTH_SHORT).show();
}
});
}
}複製程式碼