Android-LitePal データベース フレームワーク

LitePal は、オープンソースの Android データベース フレームワークです。オブジェクト リレーショナル マッピング (ORM) モードを採用して、一般的に使用されるデータベース関数をカプセル化しています。SQL を 1 行も記述することなく、テーブルの作成、追加、削除、変更、確認の操作を完了できます。発言。また、非常に軽量で、jar パッケージは 100k 未満で、構成はほとんど必要ありません。

1. はじめに

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

2. app/src/main/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>

3. LitePalApplication を構成し、AndroidManifest.xmlに追加します。

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

カスタム アプリケーションがある場合は、それをカスタム アプリケーションに追加します。

public class MyOwnApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        LitePal.initialize(this);
    }
    ...
}

4. 使用を開始する

4.1 エンティティ クラスを作成します。例:

public class Album extends LitePalSupport {
	
    @Column(unique = true, defaultValue = "unknown")
    private String name;
    
    @Column(index = true)
    private float price;

    // generated getters and setters.
    ...
}

4.2 litepal.xmlでの設定

<list>
    <mapping class="org.litepal.litepalsample.model.Album" />
</list>

4.3 基本的な使い方

// 获取对象
SQLiteDatabase db = LitePal.getDatabase();
//增
Album album = new Album();
album.setName("album");
album.setPrice(10.99f);
album.setCover(getCoverImageBytes());
album.save();
//删
LitePal.delete(Album.class, id);
//改
Album albumToUpdate = new Album();
albumToUpdate.setPrice(20.99f); // raise the price
albumToUpdate.update(id);
// 查
Song song = LitePal.find(Album.class, id);
List<Album> allAlbums = LitePal.findAll(Album.class);
List<Album> albums = LitePal.where("name like ? and duration < ?", "album%", "200").order("duration").find(Album.class);

その他の使用方法については、https: //github.com/guolindev/LitePalを参照してください。

おすすめ

転載: blog.csdn.net/Twan1234/article/details/124981337