一.什么是litepal
litepal是一种第三方的插件,用于方便开发者使用内置数据库的一种工具。
二.为什么要有litepal
因为litepal是一种数据库工具,利用它可以很轻松的解决数据库问题,减少代码量,节约时间。三.如何使用litepal
首先在添加依赖环境
compile 'org.litepal.android:core:1.6.1'
然后在project工程下找到app/src下创建一个assets文件夹,并创一个litepal.xml的file文件。如下图:
然后在litepal.xml写几行代码
<?xml version="1.0" encoding="utf-8"?>
<litepal>
<dbname value="demo" />
<dbname value="demo" />
<version value="1" />
<version value="1" />
<list>
<mapping class="org.litepal.litepalsample.model.Album" />
<mapping class="org.litepal.litepalsample.model.Song" />
</list>
</litepal>
接着在Main函数中的xml写下控件
<?xml version="1.0" encoding="utf-8"?>
<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.ll.myx.MainActivity">
<EditText
android:id="@+id/ll_et_album"
android:layout_width="match_parent"
android:layout_height="50dp" />
<Button
android:id="@+id/add_blum_cc"
android:text="添加专辑"
android:layout_width="match_parent"
android:layout_height="50dp" />
<Button
android:id="@+id/add_blum_cc1"
android:text="修改数据"
android:layout_width="match_parent"
android:layout_height="50dp" />
<Button
android:id="@+id/add_blum_cc2"
android:text="删除数据"
android:layout_width="match_parent"
android:layout_height="50dp" />
<Button
android:text="查询专辑"
android:id="@+id/chaxun_btn"
android:layout_width="match_parent"
android:layout_height="50dp" />
</LinearLayout>
最后在住Main函数写下代码
package com.example.ll.myx;
import android.annotation.SuppressLint;
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 albunet;
private Button addbtn;
private Button delbtn;
private Button xiugaibtn;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// SQLiteDatabase db = LitePal.getDatabase();
BindId();
}
@SuppressLint("WrongViewCast")
private void BindId() {
albunet = findViewById(R.id.ll_et_album);
addbtn = findViewById(R.id.add_blum_cc);
xiugaibtn = findViewById(R.id.add_blum_cc1);
delbtn = findViewById(R.id.add_blum_cc2);
btn = findViewById(R.id.chaxun_btn);
addbtn.setOnClickListener(this);
xiugaibtn.setOnClickListener(this);
delbtn.setOnClickListener(this);
btn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.add_blum_cc:
String cmc = albunet.getText().toString();
float price = 109.9f;
Album album = new Album();
album.setName(cmc);
album.setPrice(price);
album.save();//保存至数据库
break;
case R.id.add_blum_cc1:
Album album1 = DataSupport.find(Album.class, 1);
album1.setName("cmc");
album1.save();
break;
case R.id.add_blum_cc2:
// int row=DataSupport.delete(Album.class,1);//只是删除一行数据
int row = DataSupport.deleteAll(Album.class, "id>?", "3");//删除3行数据
Toast.makeText(this, "您删除了" + row + "行数据", Toast.LENGTH_SHORT).show();
break;
case R.id.chaxun_btn:
// List<Album> albumList=DataSupport.findAll(Album.class);
List<Album> albumList = DataSupport.where("id<?", "4").find(Album.class);
for (Album a :
albumList) {
Log.e("main", a.getName() + "********");
}
break;
}
}
}