LitePal

LitePal是一款开源的Android数据库框架,它采用关系映射(ORM)的模式,并将我们平时开发最常用的一些数据库功能进行了封装,使得我们不用编写一行SQL语句就能完成各种建表和增删改查的操作。LitePal的项目主页上也有着详细的使用文档,地址是:https://github.com/LitePalFramework/LitePal

比SQLite简单的多的多的数据库LitePal的使用方法:
第一步:编辑app/build.gradle文件,在dependenceies闭包中添加如下内容:implementation ‘org.litepal.android:core:1.6.1’

android 与数据库 android 数据库app_LitePal


第二步:配置litepal.xml文件,在project模式下,右击app/src/main目录---->New----->Directory,创建一个assets目录。然后在此目录下新建一个litepal.xm文件。


android 与数据库 android 数据库app_LitePal_02


接着编辑litepal.xm中内容:


android 与数据库 android 数据库app_SQLite_03


最后一步:


在Manifest.xml文件中配置一下

android:name="org.litepal.LitePalApplication"

android 与数据库 android 数据库app_数据库_04

至此Litepal的配置已经全部完成,下面开始启用它吧。因为LitePal采用的是对象映射(ORM)的模式——我们的编程语言采用的是面向对象语言,而使用的数据库是关系型数据库,那么将面向对象语言与面向关系数据库之间建立一种映射关系就叫做对象关系映射了。

它赋予了我们一个强大的功能,就是可以用面向对象的思维来操作数据库了,而不用再和SQL语句打交道了。比如我们想创建一张Book表,只需要定义一个Book类:

android 与数据库 android 数据库app_Android_05


在将Book类添加到映射模型列表中,修改litepal.xml中的代码:

<?xml version="1.0" encoding="utf-8"?>
<litepal>
    <dbname value="BookStore"></dbname>
    <version value="1"></version>
    <list>
        <mapping class="com.example.chapter_final.Book"></mapping>//建立Book表语句
    </list>
</litepal>

所有工作完成,现在只需要进行任意一次数据库操作,BookStore.db数据库就会自动创建出来。在Mainactivity.class的OnCreat()方法下添加代码 LitePal.getDatabase() ;数据库就会创建出来了。

--------------------@@@@@@@@@@@@@@@@@@@@@@@@@@------------------------------------------------------

自此我做了个小demo归纳总结了一下以上知识点及litepal数据库的增删改查等常规操作:

(个人感觉真的比SQLite方便了不下百倍)

说明:创建了个数据库BookStore.db,建立了个book表,在里面添加了一条数据

android 与数据库 android 数据库app_LitePal_06


android 与数据库 android 数据库app_SQLite_07

Book.class:

package com.example.chapter_final;

import org.litepal.crud.DataSupport;

public class Book extends DataSupport {
    //表格所拥有属性
    private int id;
    private String author;
    private double price;
    private int pages;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getPages() {
        return pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

assets目录下的litepal.xml:

<?xml version="1.0" encoding="utf-8"?>
<litepal>
    <dbname value="BookStore"></dbname>
    <version value="1"></version>
    <list>
        <mapping class="com.example.chapter_final.Book"></mapping>
    </list>
</litepal>

manifest文件下添加:

android:name="org.litepal.LitePalApplication"

MainActivity.class

package com.example.chapter_final;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.litepal.LitePal;//litepal包的导入
import org.litepal.crud.DataSupport;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button bun_creat;
    private Button bun_insert;
    private Button bun_deleate;
    private Button bun_update;
    private Button bun_find;
    private TextView txt_txt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        bun_creat = (Button) findViewById(R.id.bun_creat);
        bun_insert = (Button) findViewById(R.id.bun_insert);
        bun_deleate = (Button) findViewById(R.id.bun_deleate);
        bun_update = (Button) findViewById(R.id.bun_update);
        bun_find = (Button) findViewById(R.id.bun_find);
        txt_txt = (TextView) findViewById(R.id.txt_txt);
        bun_creat.setOnClickListener(this);
        bun_insert.setOnClickListener(this);
        bun_deleate.setOnClickListener(this);
        bun_update.setOnClickListener(this);
        bun_find.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bun_creat:
                LitePal.getDatabase();
                break;
            case R.id.bun_insert:
                Book book = new Book();
                //为book表中的属性插入数据
                book.setAuthor("D");
                book.setName("Old man");
                book.setPages(45);
                book.setPrice(15.6);
                book.save();
                break;
            case R.id.bun_deleate:
                DataSupport.deleteAll(Book.class, "price=?", "15.6");//删除book表中所有price为15.6的元素
                break;
            case R.id.bun_update:
                Book book2 = new Book();
                book2.setPages(4);
                book2.setPrice(166);
                book2.updateAll("author=?", "D");//更改book表中所有author叫D的pages和price
                break;
            case R.id.bun_find:
                StringBuilder stringBuilder = new StringBuilder();
                List<Book> books = DataSupport.findAll(Book.class);//查找整个book表中的所有元素
                for (Book book1 : books) {
                    stringBuilder.append(book1.getAuthor() + book1.getName() + book1.getPages() + book1.getPrice() + "\n");
                }
                txt_txt.setText(stringBuilder);//将查到的元素set到我事先创建好的textview上
                break;
        }
    }
}

布局:

<?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">

    <Button
        android:id="@+id/bun_creat"
        android:text="创建"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/bun_insert"
        android:text="增加"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/bun_deleate"
        android:text="删除"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/bun_update"
        android:text="改动"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/bun_find"
        android:text="查找"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/txt_txt"
        android:textSize="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>