1. 程式人生 > >android資料庫框架LitePal的簡單使用

android資料庫框架LitePal的簡單使用

GitHub地址:https://github.com/LitePalFramework/LitePal

步驟1:下載

compile 'org.litepal.android:core:1.4.0'

步驟2:

配置檔案litepal.xml 在 assets 資料夾

<?xml version="1.0" encoding="utf-8"?>
<litepal>
    <dbname value="demo" ></dbname> <!--資料庫名稱-->

    <!--
       版本號
    -->
    <version
value="1" ></version> <!-- 對映實體類 <list> <mapping class="com.test.model.Reader"></mapping> <mapping class="com.test.model.Magazine"></mapping> </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"></storage> --> </litepal>

步驟3:配置LitePalApplication

<manifest>
    <application
        android:name="org.litepal.LitePalApplication"
        ...
    >
    ...
    </application>
</manifest>
或者自己重寫的的Application
<manifest>
    <application
        android:name="com.example.MyOwnApplication"
        ...
    >
    ...
    </application>
</manifest>

public class MyOwnApplication extends AnotherApplication {

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

使用: 1、建立表
public class Album extends DataSupport {

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

    private float price;

    private byte[] cover;

    private List<Song> songs = new ArrayList<Song>();

    // generated getters and setters.
    ...
}

public class Song extends DataSupport {

    @Column(nullable = false)
    private String name;

    private int duration;

    @Column(ignore = true)
    private String uselessField;

    private Album album;

    // generated getters and setters.
    ...
}
在litepal.xml 新增對映
<list>
    <mapping class="org.litepal.litepalsample.model.Album"></mapping>
    <mapping class="org.litepal.litepalsample.model.Song"></mapping>
</list>

2、獲取資料庫  必須使用否則報空錯誤 放在application或第一個啟動的activity中都可以
SQLiteDatabase db = LitePal.getDatabase();

3、更新表 只需要修改Model
public class Album extends DataSupport {

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

    @Column(ignore = true)
    private float price;

    private byte[] cover;

    private Date releaseDate;

    private List<Song> songs = new ArrayList<Song>();

    // generated getters and setters.
    ...
}

更新表後,然後增加litepal.xml中的版本號:
<version value="2" ></version>

3、儲存資料
Album album = new Album();
album.setName("album");
album.setPrice(10.99f);
album.setCover(getCoverImageBytes());
album.save();
Song song1 = new Song();
song1.setName("song1");
song1.setDuration(320);
song1.setAlbum(album);
song1.save();
Song song2 = new Song();
song2.setName("song2");
song2.setDuration(356);
song2.setAlbum(album);
song2.save();

4、更新資料
Album albumToUpdate = DataSupport.find(Album.class, 1);
albumToUpdate.setPrice(20.99f); // raise the price
albumToUpdate.save();
具體的id更新
Album albumToUpdate = new Album();
albumToUpdate.setPrice(20.99f); // raise the price
albumToUpdate.update(id);

使用條件更新多條
Album albumToUpdate = new Album();
albumToUpdate.setPrice(20.99f); // raise the price
albumToUpdate.updateAll("name = ?", "album");

5、刪除資料
DataSupport.delete(Song.class, id);

刪除全部
DataSupport.deleteAll(Song.class, "duration > ?" , "350");

6、查詢資料 通過id查詢1條資料
Song song = DataSupport.find(Song.class, id);
查詢所有資料
List<Song> allSongs = DataSupport.findAll(Song.class);
複雜查詢
List<Song> songs = DataSupport.where("name like ?", "song%").order("duration").find(Song.class);