日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

LitePal的简单使用

發布時間:2023/12/10 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LitePal的简单使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.關于Litepal

采用ORM(對象關系映射)的模式,體積小;Litepal相比較Android原生操作Sqlite使用起來非常方便,沒有比較多步驟,主要工作量在于第一次使用往項目中引入Litepal,后期的使用是比較簡單的。下面我們一起學習吧!

地址:https://github.com/LitePalFramework/LitePal

2.集成Litepal環境

2.1 引入依賴

implementation 'org.litepal.guolindev:core:3.2.3'

2.2 配置Application

讓程序application 繼承 org.litepal.LitePalApplication

  • 第一種方法:直接將程序的application定義為LitePalApplication

    <applicationandroid:name="org.litepal.LitePalApplication"...> </application>
  • 第二種方法:手寫一個繼承LitePalApplication的類作為程序的appliction

    import org.litepal.LitePalApplication;public class BaseApplication extends LitePalApplication {}

    設置BaseApplication為程序Application

    <applicationandroid:name="com.jw.firstapp.application.BaseApplication"...> </application>
  • 2.3 新建assets目錄,在assets目錄中新建litepal.xml

  • 新建assets目錄

    在main目錄下右擊鼠標選New,然后選Folder,然后選Assets Folder

    不用選中下面的框,直接Finish

  • 新建litepal.xml

    右擊assets目錄,新建一個File,名稱命名為litepal.xml

  • ? litepal.xml文件內容:

    <?xml version="1.0" encoding="utf-8" ?> <litepal> <!--數據庫的名稱 --><dbname value="first_db"></dbname><!-- 數據的版本,后續升級需要改動版本號 --><version value="1"></version><!-- 管理數據庫中的表,一個類對應的一個表,暫時為空,后面增加表需要添加對應的類進來 --><list></list> </litepal>

    ? 到這里,配置工作就完成了!

    3.使用Litepal創建表

    3.1 新建實體類Student

    public class Student {private int id;private String name;private int age;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;} }

    3.2 在litepal.xml中增加映射關系

    <?xml version="1.0" encoding="utf-8" ?> <litepal><!--數據庫的名稱 --><dbname value="first_db"></dbname><!-- 數據的版本,后續升級需要改動版本號 --><version value="1"></version><!-- 管理數據庫中的表,一個類對應的一個表 --><list><mapping class="com.jw.firstapp.db.mode.Student"></mapping></list> </litepal>

    現在只要你對數據庫有任何的操作,Student表就會被自動創建出來;如下,獲取SQLiteDataBase實例方法:

    //執行該方法后,表就會被新建出來 SQLiteDatabase db = Connector.getDatabase();

    4.Litepal的增刪改

    4.1存儲數據

  • 預備工作,讓需要存儲的類繼承LitepalSupport,這樣就可以調用LitepalSupport的用來存儲數據的save方法了;

    public class Student extends LitePalSupport { private int id;private String name;private int age;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Student(String name, int age) {this.name = name;this.age = age;} }
  • 操作

    • 保存一個對象 bolean save()

      Student xiaoming = new Student("小明", 12); //result代表save方法的結果 boolean result = xiaoming.save();
    • 保存一組對象 boolean saveAll(Collection c)

      ArrayList<Student> students = new ArrayList<>(); for (int i = 0; i < 5; i++) {Student student = new Student("小學生" + i, new Random().nextInt(15) + 1);students.add(student); } //result代表saveAll方法的結果 boolean result = LitePal.saveAll(students); return result;
  • 4.2修改數據

    修改數據和添加數據的使用基本上一樣,也是使用API,只是吧save和saveAll變成了update和updateAll。

    4.2.1 條件的寫法

    指定條件:下面會使用到指定條件,操作符合條件的數據;這里的條件需要主要一下使用方法;在Litepal指定條件使用字符串和占位符(?)完成的;比如指定id為3的數據,寫作(“id = ?”,“3”),指定id為5并且age=10的數據,寫作(“id = ? and age = ?”,“5”,“20”)。

    總結一下,這里的?表示占用一個位置,然后在后面跟著的字符串給它賦值;特點就是一個?對應一個值;

    4.2.2 操作

    • 通過ID修改一條記錄

      //API: int update(int id); //設置id為1的記錄的名稱為“小明改名字了” Student student = new Student("小明改名字了", 15); //受影響的行數 int effectRows = student.update(1);
    • 修改所有表里所有數據

      //API:int updateAll(); //將所有的記錄的age都設置成0 Student student = new Student(); student.setAge(0); //受影響的行數 int effectRows = student.updateAll();
    • 指定一個條件,修改符合條件的數據

      //API:int updateAll(String conditions...) //將表中age=12的記錄的age設置成0Student student = new Student(); student.setAge(0); //受影響的行數 int effectRows = student.updateAll("age = ?","12");
    • 指定多個條件,修改符合條件的數據

      //API:int updateAll(String conditions...) //將表中age=100并且id=3的記錄的age設置0Student student = new Student(); student.setAge(20); //受影響的行數 int effectRows = student.updateAll("age = ? and id = ? ","0","3");
    • 指定一個列,讓該類恢復默認值

      //將表中所有記錄的age這一列置成默認值0 Student student = new Student(); student.setToDefault("age"); //受影響的行數 int effectRows =student.updateAll();

    4.3刪除數據

    刪除數據和添加數據的使用基本上一樣,也是使用API,只是吧save和saveAll變成了delete和deleteAll。

    • 通過ID刪除一條記錄

      //刪除表中id為2的記錄 //API: int delete(Class<?> modelClass,int id); // effectRows:受影響的行數 int effectRows =LitePal.delete(Student.class,2);
    • 指定一個條件,刪除滿足條件的所有數據

      //刪除表中id大于10的記錄 //API: int deleteAll(Class<?> modelClass,String conditions); // effectRows:受影響的行數 int effectRows =LitePal.deleteAll(Student.class,"id > ?","10");
    • 刪除所有數據

      //刪除表中所有記錄 //API: int deleteAll(Class<?> modelClass); // effectRows:受影響的行數 int effectRows =LitePal.deleteAll(Student.class);

    5.使用Litepal查詢數據

    • 通過Id查詢數據

      //查詢Id為1的記錄 Student student = LitePal.find(Student.class, 1);
    • 通過一組id查詢一組數據

      //查詢表中id為1,2,3,5的所有記錄 long[] ids = new long[]{1L,2L,3L,5L}; List<Student> students = LitePal.findAll(Student.class, ids);
    • 查找表中第一個和最后一個數據

      //查詢表中id為1,2,3,5的所有記錄 long[] ids = new long[]{1L,2L,3L,5L}; List<Student> students = LitePal.findAll(Student.class, ids);
    • 根據條件查詢表中數據

      //select:指定查詢的列 List<Student> students = LitePal.select("id", "age").find(Student.class); //where:指定條件,查詢的結果都是符合該條件的 List<Student> students1 = LitePal.where("age > ?", "10").find(Student.class); //order: 指定排序,desc:降序;asc:升序 //按年齡降序 List<Student> students2 = LitePal.order("age desc").find(Student.class); //limit:指定查詢的最大條數 List<Student> students3 = LitePal.limit(10).find(Student.class); //使用SQL原生語句查詢 Cursor cursor = LitePal.findBySQL("select * from student where age > 10"); while (cursor != null && cursor.moveToNext()){int age = cursor.getInt(cursor.getColumnIndex("age"));String name = cursor.getString(cursor.getColumnIndex("name"));Student student = new Student(name, age); }
    • 查詢所有數據

      List<Student> students = LitePal.findAll(Student.class);

    總結

    以上是生活随笔為你收集整理的LitePal的简单使用的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。