一.什么是LitePal?
LitePal是一款做开源的Andoid数据库框架,它采用了对象关系映射(ORM) 的模式,并将我
们平时开发最常用到的一些数据库功能进行了封装,使得不用编写一行SQL 语句就可以完成各
种建表和增剧改查的操作。LitePal 的项目主页上也有详细的使用文档,地址是https://github.com/LitePalFramework/LitePalLitePal
二.配置LitePal
要使用LitePal 的第一步,就是编辑app/buildr.grade文件,在dependencies 闭包中添加
如下内容:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
compile 'org.litepal.android:core:1.6.1'
}
这样我们就把LitePal 成功引人到当前项目中了,接下来需要配置litepal.xml 文件。右击app/src/mai 目录-New-Directory,创建一个assets 目录,然后在assets 目录下再新建一个itpalxm 文件,接着编辑litepal.xml 文件中的内容如下:
<litepal>
<dbname value="demo" />
<version value="1" />
<list>
<mapping class="com.example.administrator.litepaltest1.Album">mapping>
<mapping class="com.example.administrator.litepaltest1.Song">mapping>
list>
litepal>
最后还要在AndroidManifest.xml中配置一下LitePalApplication代码如下:
android:name="org.litepal.LitePalApplication"
之后新建类继承DataSupport:
package com.example.administrator.litepaltest1;
import org.litepal.annotation.Column;
import org.litepal.crud.DataSupport;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2018/4/2/002.
*/
public class Album extends DataSupport {
@Column(unique = true, defaultValue = "unknown")
private String name;
private float price;
private byte[] cover;
private List songs = new ArrayList();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public byte[] getCover() {
return cover;
}
public void setCover(byte[] cover) {
this.cover = cover;
}
public List getSongs() {
return songs;
}
public void setSongs(List songs) {
this.songs = songs;
}
}
package com.example.administrator.litepaltest1;
import org.litepal.annotation.Column;
import org.litepal.crud.DataSupport;
/**
* Created by Administrator on 2018/4/2/002.
*/
public class Song extends DataSupport {
@Column(nullable = false)
private String name;
private int duration;
@Column(ignore = true)
private String uselessField;
private Album album;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
public String getUselessField() {
return uselessField;
}
public void setUselessField(String uselessField) {
this.uselessField = uselessField;
}
public Album getAlbum() {
return album;
}
public void setAlbum(Album album) {
this.album = album;
}
}
之后在mainactivity中可以对两张表进行操作:
package com.example.administrator.litepaltest1;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.litepal.LitePal;
import org.litepal.crud.DataSupport;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private EditText nameET;
private Button button;
private Button mofify;
private Button delete;
private Button qurry;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SQLiteDatabase db = LitePal.getDatabase();
nameET=findViewById(R.id.name_et);
button=findViewById(R.id.name_bt);
mofify=findViewById(R.id.mofify);
delete=findViewById(R.id.delete);
qurry=findViewById(R.id.qurry);
button.setOnClickListener(this);
mofify.setOnClickListener(this);
delete.setOnClickListener(this);
qurry.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.name_bt:
String name=nameET.getText().toString();
float price=56.5f;
Album album=new Album();
album.setName(name);
album.setPrice(price);
album.save();
break;
case R.id.mofify:
Album album1= DataSupport.find(Album.class,1);
album1.setName("qwe");
album1.save();
break;
case R.id.delete:
// int row=DataSupport.delete(Album.class,1);
int row=DataSupport.deleteAll(Album.class,"id>?","3");
Toast.makeText(this, "你删除了"+row+"行", Toast.LENGTH_SHORT).show();
break;
case R.id.qurry:
// List albumList=DataSupport.findAll(Album.class);
List albumList=DataSupport.where("name like ? and id>?","q%","7").order("name").find(Album.class);
for (Album a:
albumList
) {
Log.e("Album",a.getName()+"***");
}
break;
}
}
}