Litepal使用
配置
导入依赖
在build.gradle里面添加下面依赖
dependencies {
implementation 'org.litepal.guolindev:core:3.2.3'
}
配置LitePalApplication
在AndroidManifest.xml配置LitePalApplication
<application
android:name="org.litepal.LitePalApplication"
...
>
...
</application>
遇到litepal.LitePalApplication爆红解决:
在settings.gradle添加箭头指向的代码:
配置litepal.xml
新建assets目录,在assets目录下面新建一个litepal.xml文件,加入以下配置
<?xml version="1.0" encoding="utf-8"?>
<litepal>
<!--数据库名称-->
<dbname value="Student" />
<!--数据库版本号-->
<version value="1" />
<!--用于设定所有的映射模型,即你定义数据库表的类名路径-->
<list>
</list>
</litepal>
建表
创建实体类
import org.litepal.crud.LitePalSupport;
public class Students extends LitePalSupport {
private int id;
private String name;
//需要生成get set方法
}
所有实体类都需要继承LitePalSupport
将实体类配置到映射列表
<?xml version="1.0" encoding="utf-8"?>
<litepal>
<!--数据库名称-->
<dbname value="Student" />
<!--数据库版本号-->
<version value="1" />
<!--用于设定所有的映射模型,即你定义数据库表的类名路径-->
<list>
<mapping class="com.example.lab5exer01.Students" />
</list>
</litepal>
建表
SQLiteDatabase db = Connector.getDatabase();
也可以不使用该语句,因为只要你对数据库有任何的操作,相对应的表就会被自动创建出来。
操作数据库
增
Students students=new Students();
students.setName(String.valueOf(editText.getText()));
if (students.save()) {
Toast.makeText(this, "存储成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "存储失败", Toast.LENGTH_SHORT).show();
}
删
//删除指定id的数据
LitePal.delete(Students.class,Integer.parseInt(String.valueOf(editText.getText())));
//删除表中所以数据
LitePal.deleteAll(Students.class);
//删除所以name为指定值的数据
LitePal.deleteAll(Students.class,"name=?", String.valueOf(editText.getText()));
改
//更改第一条字段为name的数据,将name改为输入的name
ContentValues contentValues=new ContentValues();
contentValues.put("name", String.valueOf(editText.getText()));
LitePal.update(Students.class,contentValues,1);
//修改全部name为指定值的数据(将name为123的数据全改为输入的name)
ContentValues contentValues=new ContentValues();
contentValues.put("name", String.valueOf(editText.getText()));
LitePal.updateAll(Students.class,contentValues,"name=?", "123");
//修改全部name数据为输入数据
ContentValues contentValues=new ContentValues();
contentValues.put("name", String.valueOf(editText.getText()));
LitePal.updateAll(Students.class,contentValues);
查
//单语句查询
Students students1= LitePal.find(Students.class,Integer.parseInt(String.valueOf(editText.getText())));
textView.setText(students1.getId()+","+students1.getName());
//所以语句查询
List<Students> students2=LitePal.findAll(Students.class);
System.out.println(students2.toString());
textView.setText(students2.toString());
升级表
当实体类发生改变时,表的结构也需要同时改变:
<?xml version="1.0" encoding="utf-8"?>
<litepal>
<!--数据库名称-->
<dbname value="Student" />
<!--数据库版本号-->
<version value="2" />
<!--用于设定所有的映射模型,即你定义数据库表的类名路径-->
<list>
</list>
</litepal>
将数据库版本号在原来的基础上+1就会更新表。
源码:
activity_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=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="增"
android:id="@+id/buttob1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="删"
android:id="@+id/buttob2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="改"
android:id="@+id/buttob3"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查"
android:id="@+id/buttob4"/>
</LinearLayout>
Mainactivity:
package com.example.lab5exer01;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;
import java.util.List;
public class MainActivity extends AppCompatActivity {
TextView textView;
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.text);
editText=findViewById(R.id.text1);
Button button1=findViewById(R.id.buttob1);
Button button2=findViewById(R.id.buttob2);
Button button3=findViewById(R.id.buttob3);
Button button4=findViewById(R.id.buttob4);
button1.setOnClickListener(this::onclick);
button2.setOnClickListener(this::onclick);
button3.setOnClickListener(this::onclick);
button4.setOnClickListener(this::onclick);
}
@SuppressLint({"NonConstantResourceId", "SetTextI18n"})
private void onclick(View view) {
switch (view.getId()){
case R.id.buttob1:
Students students=new Students();
students.setName(String.valueOf(editText.getText()));
if (students.save()) {
Toast.makeText(this, "存储成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "存储失败", Toast.LENGTH_SHORT).show();
}
Log.d("TAG", "news id is " + students.getId());
break;
case R.id.buttob2:
//删除指定id的数据
LitePal.delete(Students.class,Integer.parseInt(String.valueOf(editText.getText())));
/*//删除表中所以数据
LitePal.deleteAll(Students.class);*/
/*//删除所以name为指定值的数据
LitePal.deleteAll(Students.class,"name=?", String.valueOf(editText.getText()));*/
break;
case R.id.buttob3:
/*//更改第一条字段为name的数据,将name改为输入的name
ContentValues contentValues=new ContentValues();
contentValues.put("name", String.valueOf(editText.getText()));
LitePal.update(Students.class,contentValues,1);*/
//修改全部name为指定值的数据
ContentValues contentValues=new ContentValues();
contentValues.put("name", String.valueOf(editText.getText()));
LitePal.updateAll(Students.class,contentValues,"name=?", "123");
/*//修改全部name数据
ContentValues contentValues=new ContentValues();
contentValues.put("name", String.valueOf(editText.getText()));
LitePal.updateAll(Students.class,contentValues);*/
break;
case R.id.buttob4:
//单语句查询
Students students1= LitePal.find(Students.class,Integer.parseInt(String.valueOf(editText.getText())));
if(students1!=null){
textView.setText(students1.getId()+","+students1.getName());
}else {
textView.setText("没有该数据");
}
/*//所以语句查询
List<Students> students2=LitePal.findAll(Students.class);
System.out.println(students2.toString());
textView.setText(students2.toString());*/
break;
}
}
}
Students实体类:
package com.example.lab5exer01;
import org.litepal.crud.LitePalSupport;
public class Students extends LitePalSupport {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Students{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
private int id;
private String name;
}
版权声明:本文为qq_43761240原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。