- Notifications
You must be signed in to change notification settings - Fork7
郭大神写的数据库框架LitePal的基本使用demo,LitePal地址https://github.com/LitePalFramework/LitePal
wuxiaosu/LitePalDemo
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
郭大神写的数据库框架LitePal的基本使用demo,LitePal地址https://github.com/LitePalFramework/LitePal
dependencies { compile 'org.litepal.android:core:1.4.0'}
在assets 文件夹下添加litepal.xml :
<?xml version="1.0" encoding="utf-8"?><litepal> <!--数据库名字--> <dbname value="user" /> <!--数据库版本--> <version value="1" /> <list> <!--根据model映射创建表--> <mapping /> </list> <!--将数据库文件存放在sd卡(/sdcard/Android/data/<package name>/files/databases),调试时使用(internal,external)--> <!--<storage value="external" />--></litepal>
酱紫:
- 方式一
<manifest> <application android:name="org.litepal.LitePalApplication" ... > ... </application></manifest>
或者:
- 方式二
<manifest> <application android:name="org.litepal.MyOwnApplication" ... > ... </application></manifest>
这种方式需要继承LitePalApplication
public class MyOwnApplication extends LitePalApplication { ... }
或者:
- 方式三
public class MyOwnApplication extends AnotherApplication { @Override public void onCreate() { super.onCreate(); LitePal.initialize(this); } ...}
首先定义model(需要继承DataSupport ),酱紫:
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></mapping> <mapping></mapping></list>
表会在下次数据库操作时自动创建,也可以通过以下方式获取SQLiteDatabase实例:
SQLiteDatabase db = LitePal.getDatabase();
直接根据需要修改model(增、删字段)就好了:
public class Album extends DataSupport { @Column(unique = true, defaultValue = "unknown") private String name; //ignore @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"/>
继承DataSupport可以直接使用save() 方法,酱紫:
Album album = new Album();album.setName("album");album.setPrice(10.99f);album.setCover(getCoverImageBytes());album.save();
或者:
List<Album> newsList; ... DataSupport.saveAll(newsList);
用save() 方法修改通过find() 取出的数据:
Album albumToUpdate = DataSupport.find(Album.class, 1);albumToUpdate.setPrice(20.99f); // raise the pricealbumToUpdate.save();
或者根据id使用update() 方法修改数据:
Album albumToUpdate = new Album();albumToUpdate.setPrice(20.99f); // raise the pricealbumToUpdate.update(id);
或者通过条件使用updateAll() 方法修改多条数据:
Album albumToUpdate = new Album();albumToUpdate.setPrice(20.99f); // raise the pricealbumToUpdate.updateAll("name = ?", "album");
通过id使用DataSupport.delete() 方法删除单条记录:
DataSupport.delete(Song.class, id);
或者通过条件使用DataSupport.deleteAll() 方法删除多条记录:
DataSupport.deleteAll(Song.class, "duration > ?" , "350");
如果对象经过序列化(save()),也可以这样删除:
News news = new News(); news.setTitle("这是一条新闻标题"); news.setContent("这是一条新闻内容"); news.save(); ... news.delete();
通过id查询单条数据:
Song song = DataSupport.find(Song.class, id);
通过id查询多条数据:
List<Song> newsList = DataSupport.findAll(Song.class, 1, 3, 5, 7);
也可以这样写:
long[] ids = new long[] { 1, 3, 5, 7 }; List<Song> newsList = DataSupport.findAll(Song.class, ids);
查询第一条数据
DataSupport.findFirst(Song.class);
查询最后一条数据
DataSupport.findLast(Song.class);
查询某张表下的所有数据:
List<Song> allSongs = DataSupport.findAll(Song.class);
通过条件查询:
List<Song> songs = DataSupport.select("title", "content") .where("name like ?", "song%") .order("duration") .limit(10) .offset(10) .find(Song.class);
动态创建数据库,酱紫:
LitePalDB litePalDB = new LitePalDB("demo2", 1);litePalDB.addClassName(Singer.class.getName());litePalDB.addClassName(Album.class.getName());litePalDB.addClassName(Song.class.getName());LitePal.use(litePalDB);
或者创建一个与相同配置的数据库,酱紫:
LitePalDB litePalDB = LitePalDB.fromDefault("newdb");LitePal.use(litePalDB);
随时切换回默认数据库:
LitePal.useDefault();
删除数据库:
LitePal.deleteDatabase("newdb");
更多详情郭大神博客http://blog.csdn.net/sinyu890807/article/category/2522725
LitePal地址https://github.com/LitePalFramework/LitePal
两篇郭大神的公众号推送
我为什么能将效率提升了800%
你们要的多数据库功能终于来了
About
郭大神写的数据库框架LitePal的基本使用demo,LitePal地址https://github.com/LitePalFramework/LitePal