基于LitePal操作數(shù)據(jù)庫的學(xué)生管理系統(tǒng)的簡單實現(xiàn)

今天看了郭神的《第二行代碼》的第六章,才發(fā)現(xiàn)LitePal用起來是多么方便簡介,就花了下午的時間做了一個小Demo,界面功能都簡單,請見諒


本文只是LitePal的簡單應(yīng)用,目的是快速入門LitePal。界面和功能都簡單,若不滿,請自行添加。

照例來波動態(tài)圖


學(xué)生管理應(yīng)用.gif

主要功能就是: 數(shù)據(jù)的 增、刪、改、查。

主要知識點: LitePal的配置和使用

界面我就不介紹了,用的是RecyclerView之前也講過 簡單粗暴----RecyclerView

首先添加依賴

compile'com.android.support:recyclerview-v7:24.0.0'
compile 'org.litepal.android:core:1.4.1'

1. LitePal簡介

LitePal是一款開源的Android數(shù)據(jù)庫框架,它采用了對象關(guān)系映射(ORM)的模式,并將我們平時開發(fā)最常用的一些數(shù)據(jù)庫功能進行了封裝,是的不用編寫一行SQL語句就可以完成各種建表和增刪改查的操作。

2. LitePal的配置

創(chuàng)建一個 assets 目錄,在 assets 目錄下新建一個 litepal.xml 文件,接著編寫文件內(nèi)容,如下:

<?xml version="1.0" encoding="utf-8" ?>
<litepal>
    <!-- 數(shù)據(jù)庫名 -->
    <dbname value="Student"></dbname>
    <!-- 版本號 -->
    <version value="1"></version>
    <!-- 創(chuàng)建表 -->
    <list>
        <mapping class="映射的javaBean的完整類名"></mapping>
    </list>
</litepal>

接下來修改清單文件代碼,配置Application

<application
    android:name="org.litepal.LitePalApplication"
    ...
</application>

最后代碼中創(chuàng)建數(shù)據(jù)庫

LitePal.getDatabase();

3. 創(chuàng)建表

a. 需要一個JavaBean對象,也就是數(shù)據(jù)庫的表


public class Student extends DataSupport{

    private int id;
    private String name;//姓名
    private int studentId;//學(xué)號
    private String sex;//性別

    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 getStudentId() {
        return studentId;
    }

    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

b. 修改litepal.xml中的代碼


<list>
    <mapping class="com.xiaweizi.studentsystem.Student"></mapping>
</list>

運行一下程序,然后你就可以在data/data/包名的文件下看到數(shù)據(jù)庫已經(jīng)創(chuàng)建了:

數(shù)據(jù)庫.PNG

然后把他導(dǎo)出來,借用工具打開,然后就是下面界面

表的創(chuàng)建.PNG

4. 增、刪、改、查

1. 添加數(shù)據(jù)

    Student student = new Student();
    student.setName(name);
    student.setSex(sex);
    student.setStudentId(Integer.parseInt(studentId));
    student.save();

效果如下:


添加數(shù)據(jù).PNG

2. 刪除數(shù)據(jù)

DataSupport.deleteAll(Student.class, "id = ?", id +"");

一行代碼搞定,只要調(diào)用DataSupport.deleteAll()即可,第一參數(shù),是要刪除哪張表的數(shù)據(jù),后面則為約束條件,不難看懂。

3. 修改數(shù)據(jù)

    Student student = new Student();
    student.setName(name);
    student.setSex(sex);
    student.setStudentId(Integer.parseInt(studentId));
    student.updateAll("id = ?", id+"");

還是要new一個實例,然后要設(shè)置更新的數(shù)據(jù),最后調(diào)用updateAll()方法執(zhí)行更新操作。參數(shù)跟刪除數(shù)據(jù)很像,也是約束條件,如果不傳參數(shù),則修改所有的數(shù)據(jù)。

這里需要注意一點,如果想讓數(shù)據(jù)恢復(fù)成默認值,是不能直接設(shè)置默認值的。

比如,如果讓學(xué)號為0,是不能 student.setStudentId(0); 這是錯誤的?。。∧敲慈绻牖謴?fù)成默認值該怎么辦呢,LitePal提供了setToDefault() 方法。

Student student = new Student();
student.setToDefault("studentId");
student.updateAll();

4. 查詢數(shù)據(jù)

mList = DataSupport.findAll(Student.class);

一行代碼搞定,直接就可以查詢數(shù)據(jù)庫中Student表的所有數(shù)據(jù),返回這個對象的集合。
借用工具可以查看我們的所有數(shù)據(jù):


所有數(shù)據(jù).PNG

到此一個簡單的學(xué)生管理系統(tǒng)已經(jīng)結(jié)束了,主要目的就是快速入門LitePal!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,828評論 25 709
  • 內(nèi)容抽屜菜單ListViewWebViewSwitchButton按鈕點贊按鈕進度條TabLayout圖標下拉刷新...
    皇小弟閱讀 47,136評論 22 665
  • 原文鏈接:https://github.com/opendigg/awesome-github-android-u...
    IM魂影閱讀 33,143評論 6 472
  • 7-9(1)遇見 遇見、偶遇,在上億人海中遇見、偶遇是件多么美妙的事情。有時候很是疑惑,為什么會和A相遇而不是B,...
    x123閱讀 353評論 0 0
  • 這是一道很經(jīng)典的題目,相信很多程序員都知道。 其實解題的思路很簡單,就是在每行枚舉選擇每個位置,判斷選則的位置是否...
    ShutLove閱讀 394評論 0 0

友情鏈接更多精彩內(nèi)容