前言:每次都要创建数据库数据表,不仅繁琐而且及其容易出错。所以接下来就要轮到LitePal数据库插件登场了。
注意点:由于是插件,代码量大,而且难度较大。所以可以直接登录Githap官方网站要进行借鉴学习
LitePal使用实例(以增删改查专辑音乐为例)
1.首先在Gradle Scripts里你自己新建的Activity的build.gradle里添加LitePal的依赖包,然后下载一下
compile 'org.litepal.android:core:1.6.1'
2.然后在src/main里创建一个assets包,在里面定义一个litepal.xml文件。在里面写以下方法
<?xml version="1.0" encoding="utf-8"?>
<litepal>
<dbname value="demo" />
<version value="1" />
<list>
<mapping class="com.example.litepal.Album" />
<mapping class="com.example.litepal.Song" />
</list>
</litepal>
3.接着分别创建album和song两个实体类,在里面定义属性和方法
Album
public class Album extends DataSupport {
@Column(unique = true, defaultValue = "unknown")
private String name;
private float price;
private byte[] cover;
private List<Song> songs = new ArrayList<Song>();
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<Song> getSongs() {
return songs;
}
public void setSongs(List<Song> songs) {
this.songs = songs;
}
}
Song
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;
}
}
4.回到layout布局文件里,定义出必要的一些控件
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.litepal.MainActivity">
<EditText
android:id="@+id/album_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="歌曲名"/>
<Button
android:id="@+id/album_insert_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加到专辑"/>
<Button
android:id="@+id/album_dele_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="删除"/>
<Button
android:id="@+id/album_update_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="修改"/>
<Button
android:id="@+id/album_query_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询"/>
</LinearLayout>
5.在主体类里定义出这些控件,绑定ID,设置监听事件
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText albumET;
private Button albuminsertbtn;
private Button albumdeleBtn;
private Button albumupdateBtn;
private Button albumqueryBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// SQLiteDatabase db = LitePal.getDatabase();
bindID();
}
private void bindID() {
albumET = findViewById(R.id.album_et);
albuminsertbtn = findViewById(R.id.album_insert_btn);
albumdeleBtn = findViewById(R.id.album_dele_btn);
albumupdateBtn = findViewById(R.id.album_update_btn);
albumqueryBtn=findViewById(R.id.album_query_btn);
albuminsertbtn.setOnClickListener(this);
albumdeleBtn.setOnClickListener(this);
albumupdateBtn.setOnClickListener(this);
albumqueryBtn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
6.最后就是在增删改查四个方法里进行对应的操作
增
case R.id.album_insert_btn:
String albumName = albumET.getText().toString();
float price = 109.9f;
Album album = new Album();
album.setName(albumName);
album.setPrice(price);
album.save();//保存进数据库
break;
删
case R.id.album_dele_btn:
int row = DataSupport.deleteAll(Album.class, "id>?", "2");
Toast.makeText(this, "您删除了" + row + "行数据", Toast.LENGTH_SHORT).show();
break;
改
case R.id.album_update_btn:
Album album1 = DataSupport.find(Album.class, 1);
album1.setName("bue");
album1.save();
break;
查
case R.id.album_query_btn:
// List<Album> albumList=DataSupport.findAll(Album.class);
// List<Album> albumList=DataSupport.where("name like ?","a%").find(Album.class);
List<Album> albumList=DataSupport.where("id<? and name like ?","9","a%").find(Album.class);
for(Album a:
albumList){
Log.e("MAIN",a.getName()+"****");
}
break;
笔芯!