Litepal的使用
数据库的创建,dao类的编写,很繁琐。直到使用了Litepal,一切都轻松了很多。
1,引入依赖
compile ‘org.litepal.android:core:1.5.1’
2,配置litepal.xml。在assets文件夹下 创建litepal.xml (文件名要相同)设置数据库的文件名,版本号,声明要操作的bean类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| <?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,配置Application。没有自己的Application,就使用
android:name=”org.litepal.LitePalApplication”
有就在onCreate()中初始化LitePal
1
| LitePal.initialize(this);
|
创建bean类时,让其继承DataSupport。这里一个bean类就是一张表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| 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. ... }
|
增
添加数据:创建一个bean对象,调用set方法设置数据,最后调用save方法添加数据。
1 2 3 4 5
| Album album = new Album(); album.setName("album"); album.setPrice(10.99f); album.setCover(getCoverImageBytes()); album.save();
|
改
1 2 3
| Album albumToUpdate = new Album(); albumToUpdate.setPrice(20.99f); // raise the price albumToUpdate.update(id);
|
查
查询单个数据
1
| Song song = DataSupport.find(Song.class, id);
|
查询表数据
1
| List<Song> allSongs = DataSupport.findAll(Song.class);
|
条件查询
1
| List<Song> songs = DataSupport.where("name like ?", "song%").order("duration").find(Song.class);
|
删
单个删除
1
| DataSupport.delete(Song.class, id);
|
条件删除
1
| DataSupport.deleteAll(Song.class, "duration > ?" , "350");
|
混淆
1 2 3 4 5 6 7
| -keep class org.litepal.** { *; } -keep class * extends org.litepal.crud.DataSupport { *; }
|