Android LitePal介绍与使用说明
LitePal for Android
LitePal是一个Android开源库,它使开发者使用SQLite数据库变得非常容易。 你可以不用写一句SQL语句就可以完成大部分数据库操作,包括创建表,更新表,约束操作,聚合功能等等。LitePal的安装也相当简单,5分钟之内就可以将它集成到你的工程里。
功能
- 使用对象关系映射(ORM) 模型。
- 几乎零配置(只有一个配置文件,该配置文件属性很少)。
- 自动维护所有表格(比如创建、更改、删除表格)。
- 提供封装的API,无需写SQL语句。
- 很棒的集群查询功能。
- 依然可以选择使用SQL,LitePal提供比原始更易用更好的API接口。
最新下载
- litepal-1.3.0.jar (库包含*.class文件)
- litepal-1.3.0-src.jar (库包含*.class和*.java文件)
快速安装
1. 导入库
使用Eclipse
- 下载最新的jar,也可下载历史其他版本。
- 将jar放到工程里的库文件夹里。
使用Android Studio
编辑build.gradle文件并添加以下依赖说明:
- dependencies {
- compile 'org.litepal.android:core:1.3.0'
- }
2. 配置litepal.xml
在工程里的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 automaticly for you.
- For example:
- <dbname value="demo" ></dbname>
- -->
- <dbname value="demo" ></dbname>
- <!--
- 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 automaticly without concern.
- For example:
- <version value="1" ></version>
- -->
- <version value="1" ></version>
- <!--
- 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>
- <mapping class="com.test.model.Magazine"></mapping>
- </list>
- -->
- <list>
- </list>
- </litepal>
这是唯一的一个配置文件,里面的属性很简单。
- dbname用于配置工程的数据库文件名。
- version用于配置数据库的版本信息。每次升级数据库,该版本号加1。
- list用于配置映射类。
3. 配置LitePalApplication
操作数据库时需要使用到Context参数,我们不想每次都传递这个参数,那么只需要在AndroidManifest.xml中配置下LitePalApplication即可,如下:
- <manifest>
- <application
- android:name="org.litepal.LitePalApplication"
- ...
- >
- ...
- </application>
- </manifest>
当然,你可能有自己的Application并且已经配置好,如下:
- <manifest>
- <application
- android:name="com.example.MyOwnApplication"
- ...
- >
- ...
- </application>
- </manifest>
没关系,只需要将MyOwnApplication由原来的继承Application类改成继承LitePalApplication类就可以,如下:
- public class MyOwnApplication extends LitePalApplication {
- ...
- }
如果你的MyOwnApplication必须继承另外的Application类,如AnotherApplication类,那么你可以直接调用LitePalApplication.initialize(context)而无需继承LiteApplication类,如下:
- public class MyOwnApplication extends AnotherApplication {
- @Override
- public void onCreate() {
- super.onCreate();
- LitePalApplication.initialize(this);
- }
- ...
- }
LitePalApplication.initialize(context)的调用原则是尽可能早,比如合适的调用位置是在Application的onCreate()里调用。调用时传递的参数是Application的context,不要使用任何activity或service的实例作为参数,否则可能发生内存泄漏。
开始LitePal体验之旅
1. 创建表格
首先定义各种model,比如有两个model:Album和Song,定义如下:
- public class Album extends DataSupport {
- @Column(unique = true, defaultValue = "unknown")
- private String name;
- private float price;
- 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.
- ...
- }
将这两个model添加到litepal.xml的映射表中,如下:
- <list>
- <mapping class="org.litepal.litepalsample.model.Album"></mapping>
- <mapping class="org.litepal.litepalsample.model.Song"></mapping>
- </list>
一旦操作数据库时,数据库表格将自动生成。比如使用以下代码获取SQLiteDatabase时,
- SQLiteDatabase db = Connector.getDatabase();
将自动生成album和song两张数据库表格,如下:
- CREATE TABLE album (
- id integer primary key autoincrement,
- name text unique default 'unknown',
- price real
- );
- CREATE TABLE song (
- id integer primary key autoincrement,
- name text not null,
- duration integer,
- album_id integer
- );
2. 升级表格
在LitePal中实现升级表格非常容易。
- public class Album extends DataSupport {
- @Column(unique = true, defaultValue = "unknown")
- private String name;
- @Column(ignore = true)
- private float price;
- private Date releaseDate;
- private List<Song> songs = new ArrayList<Song>();
- // generated getters and setters.
- ...
- }
上述代码中,添加了releaseDate并且price标注为ignore。
- <!--
- 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 automaticly without concern.
- For example:
- <version value="1" ></version>
- -->
- <version value="2" ></version>
只需要在litepal.xml中升级版本号,那么下次操作数据库时表格将自动升级:ablum表中添加了releasedate列,删除了price列,其他列的数据原封不动。
以下一些升级情况LitePal无法处理并且被升级表格里的所有数据将被清空:
- 添加了一个标注为
unique = true 的属性;
- 修改某个属性的标注为
unique = true;
- 修改某个属性的标注为
nullable = false;
以上情况会导致数据丢失,要格外注意。
3. 保存数据
每一个继承DataSupport类的model都有save()方法。
- Album album = new Album();
- album.setName("album");
- album.setPrice(10.99f);
- 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();
以上代码实现将album, song1和song2插入到数据库中并建议关联。
4. 更新数据
继承DataSupport类的每一个model都有update()和updateAll()方法。update()可更新指定id的单条记录,如下:
- Album albumToUpdate = new Album();
- albumToUpdate.setPrice(20.99f); // raise the price
- albumToUpdate.update(id);
updateAll()可同时更新满足一定条件的多条记录,如下:
- Album albumToUpdate = new Album();
- albumToUpdate.setPrice(20.99f); // raise the price
- albumToUpdate.updateAll("name = ?", "album");
5. 删除数据
调用DataSupport的静态方法delete()可删除指定id的单条记录:
- DataSupport.delete(Song.class, id);
也可调用静态方法deleteAll()删除多条记录:
- DataSupport.deleteAll(Song.class, "duration > ?" , "350");
6. 查询数据
查询song表中指定id的单条记录:
- Song song = DataSupport.find(Song.class, id);
查询song表中的所有记录:
- List<Song> allSongs = DataSupport.findAll(Song.class);
构建复杂的集群查询:
- List<Song> songs = DataSupport.where("name like ?", "song%").order("duration").find(Song.class);
Android LitePal介绍与使用说明的更多相关文章
- android Animation介绍
Animation介绍: 在Android SDK介绍了2种Animation模式: 1. Tween Animation:间动画,通过对场景里的对象不断做图像变换(平移.缩放.旋转)产生动画效果,即 ...
- android AsyncTask介绍(转)
android AsyncTask介绍 AsyncTask和Handler对比 1 ) AsyncTask实现的原理,和适用的优缺点 AsyncTask,是android提供的轻量级的异步类,可以直接 ...
- Android monkey介绍
Android monkey介绍 原文地址 1 简略 monkey是android下自动化测试比较重要的的一个工具,该工具可以运行在host端或者设备(模拟器或真实设备).它会向系统发送随机事件流(即 ...
- [Learn Android Studio 汉化教程]第一章 : Android Studio 介绍
注:为了看上去比较清晰这里只转载了中文 原地址: [Learn Android Studio 汉化教程]第一章 : Android Studio 介绍 本章将引导您完成安装和设置开发环境,然后你就可 ...
- 【转】Android bluetooth介绍(三): 蓝牙扫描(scan)设备分析
原文网址:http://blog.csdn.net/xubin341719/article/details/38584469 关键词:蓝牙blueZ A2DP.SINK.sink_connect.s ...
- Android bluetooth介绍(四): a2dp connect流程分析
关键词:蓝牙blueZ A2DP.SINK.sink_connect.sink_disconnect.sink_suspend.sink_resume.sink_is_connected.sink_ ...
- android动画介绍之 自己定义Animation动画实现qq抖一抖效果
昨天我们介绍了Animation的基本使用方法.小伙伴们了解的怎么样了?假设还没有了解过Animation的小伙伴能够看看这篇博客 android动画介绍--Animation 实现loading动画 ...
- android动画介绍之 自定义Animation动画实现qq抖一抖效果
昨天我们介绍了Animation的基本用法.小伙伴们了解的怎么样了?如果还没有了解过Animation的小伙伴可以看看这篇博客 android动画介绍--Animation 实现loading动画效果 ...
- android动画介绍--Animation 实现loading动画效果
Animation的使用方法并不难.这里简单的介绍一下使用方法. 先看效果图: 效果还是不错的吧. 下面来看看使用方法. 动画效果是通过Animation来实现的,一共有四种,分别为: AlphaAn ...
随机推荐
- 二、mysql安装详解
step1:打开下载的mysql安装文件(mysql-5.5.36-win32.msi),双击运行,如下图: step2:点击“Next”按钮继续,如下图: step3:点击“勾选”,点击“Next” ...
- struts2 ValueStack的set方法与setValue方法的区别
struts2中 ValueStack的set方法与setValue方法的区别呢? 示例代码: ActionContext.getContext().getValueStack().setValue( ...
- Ubuntu 安装 texlive
下载网站: http://tug.org/texlive/acquire-netinstall.html 此处解释texlive配置PATH gedit ~/.bashrc 在文件最后添加以下内容, ...
- K个联通块
题意: 有一张无重边的无向图, 求有多少个边集,使得删掉边集里的边后,图里恰好有K个联通块. 解法: 考虑dp,$h(i,S)$表示有$i$个联通块,点集为$S$的图的个数,$g(S)$表示点集为S的 ...
- 1.5 Hive初步使用和安装MySQL
一.HQL初步试用 1.创建一个student表 #创建一个student表 hive> create table student(id int, name string) ROW FORMAT ...
- c++函数模板二栈实现
1 没有使用模板的栈实现 #include <iostream> #include <string> using namespace std; class Stack { pu ...
- c语言编译器内置宏
注:转自http://www.cnblogs.com/lixiaohui-ambition/archive/2012/08/21/2649052.html 感谢分享 前言: 我们在写程序的时候,总是 ...
- 2019ICPC西安邀请赛 - B. Product - 数论
打印的时候麻烦把:https://blog.csdn.net/skywalkert/article/details/50500009这个打印下来. 求\(\prod\limits_{i=1}^{n} ...
- CodeForces 644B【模拟】
题意: 查询数 和 最大的队列容量+1: 按时间顺序 ti代表,第i个出线的时间: di代表,第i个需要处理的时间: 对于第i个输出他所需要的时间完成,或者拒绝进入输出-1: 思路: 真是MDZZ了, ...
- 聊聊IT行业加班的问题
IT行业(包括互联网行业)是快速发展的行业,有时候一家公司同时可能要开发多个项目,并发进行,在公司开发人员相对固定的情况下,要想在指定的时间内完成项目谈何容易. 项目多.任务重.需求的不明确.技术难关 ...