Android数据存储_LitePal

        LitePal是一款开源的Android数据库框架,它采用了对象关系映射(ORM)的模式,并将我们平时开发时最常用到的一些数据库功能进行了封装,使得不用编写一行SQL语句就可以完成各种建表、増删改查的操作。并且LitePal很“轻”,jar包只有100k不到,而且近乎零配置,这一点和Hibernate这类的框架有很大区别。目前LitePal的源码已经托管到了GitHub上,地址是 https://github.com/LitePalFramework/LitePal

1.引入项目

dependencies {
    implementation 'org.litepal.guolindev:core:3.1.1'
}

2.配置litepal.xml

新建一个 assets文件夹,创建litepal.xml文件

<?xml version="1.0" encoding="utf-8"?>
<litepal>
    <!--
    	Define the database name of your application. 
    	By default each database name should be end with .db. 
    	If you didn't name your database end with .db, 
    	LitePal would plus the suffix automatically for you.
    	For example:    
    	<dbname value="demo" />
    -->
    <dbname value="demo" />

    <!--
    	Define the version of your database. Each time you want 
    	to upgrade your database, the version tag would helps.
    	Modify the models you defined in the mapping tag, and just 
    	make the version value plus one, the upgrade of database
    	will be processed automatically without concern.
			For example:    
    	<version value="1" />
    -->
    <version value="1" />

    <!--
    	Define your models in the list with mapping tag, LitePal will
    	create tables for each mapping class. The supported fields
    	defined in models will be mapped into columns.
    	For example:    
    	<list>
    		<mapping class="com.test.model.Reader" />
    		<mapping class="com.test.model.Magazine" />
    	</list>
    -->
    <list>
    </list>
    
    <!--
        Define where the .db file should be. "internal" means the .db file
        will be stored in the database folder of internal storage which no
        one can access. "external" means the .db file will be stored in the
        path to the directory on the primary external storage device where
        the application can place persistent files it owns which everyone
        can access. "internal" will act as default.
        For example:
        <storage value="external" />
    -->
    
</litepal>
  • dbname 数据库名称
  • version 配置数据库的版本。每次你想升级数据库时,加上这里的值。
  • list 配置映射类,BEAN类
  • storage  配置数据库文件的存储位置。只有内部和外部选项有效。

3.配置LitePal

3.1从清单文件中配置:

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

3.2从自己的Application中配置

<application
    android:name=".MyApplication"
    ...>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        LitePal.initialize(this);
    }
}

4.创建数据库

SQLiteDatabase db = LitePal.getDatabase();

android_metadata和table_schema表是自动生成的

5.创建表

public class Peodata extends LitePalSupport {

    @Column(unique = true, defaultValue = "unknown")
    private String name;
    private int age;

    public Peodata(String name, int age) {
        this.name = name;
        this.age = age;
    }

    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;
    }
}

litepal.xml

<list>
    <mapping class="com.example.mysave4.Peodata" />
</list>

已经添加了一个表,ID是默认添加的,

6.添加表

public class BookData {
    @Column(nullable = false)
    private String name;
    @Column(ignore = true)
    private int price;

    public BookData(String name, int price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

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

    public int getPrice() {
        return price;
    }

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

litepal.xml

//版本要增加

<version value="3" />

//添加表的包名

<list>
    <mapping class="com.example.mysave4.Peodata" />
    <mapping class="com.example.mysave4.BookData" />
</list>

如图图已经添加。

7.更新表

在bookdata中添加author。

public class BookData {
    @Column(nullable = false)
    private String name;
    @Column(ignore = true)
    private int price;

    private String author;
...
}

对应的

litepal.xml

//版本要增加

<version value="4" />

如图查看到的已经添加了author字段。

8.保存

Peodata peodata1 = LitePal.findLast(Peodata.class);
Peodata peodata = new Peodata();
peodata.setName("2x小1");
peodata.setId(peodata1.getId() + 1);
peodata.saveThrows();//当保存失败的时候回抛出异常
boolean save = peodata.save();//是否保存成功。
Log.d("TAG", "save:" + save);

9.修改数据

//1
Peodata peodata = LitePal.find(Peodata.class, 1);
peodata.setAge(25); // raise the price
peodata.save();

//2
Peodata peodata1 = new Peodata();
peodata1.setAge(25); // raise the price
peodata1.update(2);
//3
Peodata peodata2 = new Peodata();
peodata2.setAge(25); // raise the price
peodata2.updateAll("name = ?", "2");

10.删除数据

//1
LitePal.delete(Peodata.class, 1);
//2
LitePal.deleteAll(Peodata.class, "name = ?", "2");

 

11.查询数据

//1 id
Peodata peodata = LitePal.find(Peodata.class, 3);
Log.d("TAG","peodata:"+peodata.toString());
//2 全部
List<Peodata> peodata1 = LitePal.findAll(Peodata.class);
Log.d("TAG","--------------------------");
for (Peodata peodata2 : peodata1) {
    Log.d("TAG","peodata2:"+peodata2.toString());
}
//3
List<Peodata> peodata2 = LitePal.where("age > ? ", "0").order("duration").find(Peodata.class);
Log.d("TAG","-------------------------------------");
for (Peodata peodata3 : peodata2) {
    Log.d("TAG","peodata3:"+peodata3.toString());
}

 

 

转发表明出处https://blog.csdn.net/qq_35698774/article/details/106698065

点击下载源码

android互助群:

感谢:郭霖的《第一行代码 第二版》

感谢:https://blog.csdn.net/guolin_blog/article/details/38556989

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章