一.什么是litepal
- LitePal是Android中比较轻便的开源数据库框架,只需要简单的配置就可以使用,避免了自己创建过程中的小错误。
- LitePal提供快捷简单直观的修改数据库版本的方式,方便数据库版本更迭。
二.为什么要有litepal
LitePal是一款开源的Android数据库框架,采用了对象关系映射(ORM)的模式,将平时开发时最常用的一些数据库功能进行了封装,使得开发者不用编写一行SQL语句就可以完成各种建表、増删改查的操作。并且LitePal很“轻”,jar包大小不到100k,而且近乎零配置,这一点和Hibernate这类的框架有很大区别。目前LitePal的源码已经托管到了GitHub上。
三.如何使用litepal
1.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>
2.在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>
3.在住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;
}
}
}