Litepal使用
配置
導(dǎo)入依賴
在build.gradle里面添加下面依賴
dependencies {implementation 'org.litepal.guolindev:core:3.2.3'}配置LitePalApplication
在AndroidManifest.xml配置LitePalApplication
<applicationandroid:name="org.litepal.LitePalApplication"...>...</application>遇到litepal.LitePalApplication爆紅解決:
在settings.gradle添加箭頭指向的代碼:
配置litepal.xml
新建assets目錄,在assets目錄下面新建一個litepal.xml文件,加入以下配置
<?xml version="1.0" encoding="utf-8"?> <litepal><!--數(shù)據(jù)庫名稱--><dbname value="Student" /><!--數(shù)據(jù)庫版本號--><version value="1" /><!--用于設(shè)定所有的映射模型,即你定義數(shù)據(jù)庫表的類名路徑--><list></list> </litepal>建表
創(chuàng)建實體類
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><!--數(shù)據(jù)庫名稱--><dbname value="Student" /><!--數(shù)據(jù)庫版本號--><version value="1" /><!--用于設(shè)定所有的映射模型,即你定義數(shù)據(jù)庫表的類名路徑--><list><mapping class="com.example.lab5exer01.Students" /></list> </litepal>建表
SQLiteDatabase db = Connector.getDatabase();也可以不使用該語句,因為只要你對數(shù)據(jù)庫有任何的操作,相對應(yīng)的表就會被自動創(chuàng)建出來。
操作數(shù)據(jù)庫
增
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的數(shù)據(jù)LitePal.delete(Students.class,Integer.parseInt(String.valueOf(editText.getText())));//刪除表中所以數(shù)據(jù)LitePal.deleteAll(Students.class);//刪除所以name為指定值的數(shù)據(jù)LitePal.deleteAll(Students.class,"name=?", String.valueOf(editText.getText()));改
//更改第一條字段為name的數(shù)據(jù),將name改為輸入的nameContentValues contentValues=new ContentValues();contentValues.put("name", String.valueOf(editText.getText()));LitePal.update(Students.class,contentValues,1);//修改全部name為指定值的數(shù)據(jù)(將name為123的數(shù)據(jù)全改為輸入的name)ContentValues contentValues=new ContentValues();contentValues.put("name", String.valueOf(editText.getText()));LitePal.updateAll(Students.class,contentValues,"name=?", "123");//修改全部name數(shù)據(jù)為輸入數(shù)據(jù)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());升級表
當(dāng)實體類發(fā)生改變時,表的結(jié)構(gòu)也需要同時改變:
<?xml version="1.0" encoding="utf-8"?> <litepal><!--數(shù)據(jù)庫名稱--><dbname value="Student" /><!--數(shù)據(jù)庫版本號--><version value="2" /><!--用于設(shè)定所有的映射模型,即你定義數(shù)據(jù)庫表的類名路徑--><list></list> </litepal>?將數(shù)據(jù)庫版本號在原來的基礎(chǔ)上+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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/text" /><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/text1"/><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="增"android:id="@+id/buttob1"/><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="刪"android:id="@+id/buttob2"/><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="改"android:id="@+id/buttob3"/><Buttonandroid: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;@Overrideprotected 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的數(shù)據(jù)LitePal.delete(Students.class,Integer.parseInt(String.valueOf(editText.getText())));/*//刪除表中所以數(shù)據(jù)LitePal.deleteAll(Students.class);*//*//刪除所以name為指定值的數(shù)據(jù)LitePal.deleteAll(Students.class,"name=?", String.valueOf(editText.getText()));*/break;case R.id.buttob3:/*//更改第一條字段為name的數(shù)據(jù),將name改為輸入的nameContentValues contentValues=new ContentValues();contentValues.put("name", String.valueOf(editText.getText()));LitePal.update(Students.class,contentValues,1);*///修改全部name為指定值的數(shù)據(jù)ContentValues contentValues=new ContentValues();contentValues.put("name", String.valueOf(editText.getText()));LitePal.updateAll(Students.class,contentValues,"name=?", "123");/*//修改全部name數(shù)據(jù)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("沒有該數(shù)據(jù)");}/*//所以語句查詢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;}@Overridepublic 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;}總結(jié)
- 上一篇: (转)三层相关案例(及常见的错误)
- 下一篇: oracle中isnumber函数,Or