android数据库框架LitePal的简单使用
来源:互联网 发布:js object 添加元素 编辑:程序博客网 时间:2024/04/30 02:09
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>
<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 pricealbumToUpdate.save();具体的id更新
Album albumToUpdate = new Album();albumToUpdate.setPrice(20.99f); // raise the pricealbumToUpdate.update(id);
使用条件更新多条
Album albumToUpdate = new Album();albumToUpdate.setPrice(20.99f); // raise the pricealbumToUpdate.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);
0 0
- android数据库框架LitePal的简单使用
- litePal数据库框架简单使用
- Android数据库框架--litepal的使用
- Android LitePal数据库框架的使用详解
- Android数据库框架LitePal的使用
- Android数据库框架--litepal的使用
- 【Android 】Litepal 数据库框架使用
- Android数据库LitePal框架学习笔记(1)---数据库的adb操作以及LitePal使用入门
- android SQLite数据库开源框架LitePal的使用详解
- Android开源数据库框架-LitePal的使用
- Android 数据库框架LitePal使用详解
- Android第三方数据库框架litePal使用
- litepal---Android数据库框架
- LitePal---Android数据库框架
- Litepal 数据库操作框架的使用 (火)
- 数据库框架,litepal的配置和使用
- Android数据库之LitePal框架
- LitePal专业数据库框架使用
- Extjs-行编辑器 Ext.ux.grid.RowEditor()
- 注册下载APCAD三维开发平台
- CentOS中建立Jenkins服务器
- java NIO的模型来源
- 实体类(VO,DO,DTO)的划分
- android数据库框架LitePal的简单使用
- 根据IP地址十进制范围--获得IP地址所在地
- T语言与快速开发平台之天宇开发平台开发指南
- 为什么需要把应用中出现的文字单独存放在string.xml文件中
- 使用pickle存储数据dump 和 load
- activemq 中的 concurrentConsumers
- CString转为wchar_t
- DELL服务器装上oracle 11G怎么修改SCAN IP讲解
- 在WPF里面按Enter键得到Tab的效果