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

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