LitePal for Android

LitePal是一个Android开源库,它使开发者使用SQLite数据库变得非常容易。 你可以不用写一句SQL语句就可以完成大部分数据库操作,包括创建表,更新表,约束操作,聚合功能等等。LitePal的安装也相当简单,5分钟之内就可以将它集成到你的工程里。

功能

  • 使用对象关系映射(ORM) 模型。
  • 几乎零配置(只有一个配置文件,该配置文件属性很少)。
  • 自动维护所有表格(比如创建、更改、删除表格)。
  • 提供封装的API,无需写SQL语句。
  • 很棒的集群查询功能。
  • 依然可以选择使用SQL,LitePal提供比原始更易用更好的API接口。

最新下载

快速安装

1. 导入库

使用Eclipse
  • 下载最新的jar,也可下载历史其他版本。
  • 将jar放到工程里的库文件夹里。
使用Android Studio

编辑build.gradle文件并添加以下依赖说明:

  1. dependencies {
  2. compile 'org.litepal.android:core:1.3.0'
  3. }

2. 配置litepal.xml

在工程里的assets文件夹里新建一个litepal.xml文件,将以下代码拷贝进去。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <litepal>
  3. <!--
  4. Define the database name of your application.
  5. By default each database name should be end with .db.
  6. If you didn't name your database end with .db,
  7. LitePal would plus the suffix automaticly for you.
  8. For example:
  9. <dbname value="demo" ></dbname>
  10. -->
  11. <dbname value="demo" ></dbname>
  12.  
  13. <!--
  14. Define the version of your database. Each time you want
  15. to upgrade your database, the version tag would helps.
  16. Modify the models you defined in the mapping tag, and just
  17. make the version value plus one, the upgrade of database
  18. will be processed automaticly without concern.
  19. For example:
  20. <version value="1" ></version>
  21. -->
  22. <version value="1" ></version>
  23.  
  24. <!--
  25. Define your models in the list with mapping tag, LitePal will
  26. create tables for each mapping class. The supported fields
  27. defined in models will be mapped into columns.
  28. For example:
  29. <list>
  30. <mapping class="com.test.model.Reader"></mapping>
  31. <mapping class="com.test.model.Magazine"></mapping>
  32. </list>
  33. -->
  34. <list>
  35. </list>
  36. </litepal>

这是唯一的一个配置文件,里面的属性很简单。

  • dbname用于配置工程的数据库文件名。
  • version用于配置数据库的版本信息。每次升级数据库,该版本号加1。
  • list用于配置映射类。

3. 配置LitePalApplication

操作数据库时需要使用到Context参数,我们不想每次都传递这个参数,那么只需要在AndroidManifest.xml中配置下LitePalApplication即可,如下:

  1. <manifest>
  2. <application
  3. android:name="org.litepal.LitePalApplication"
  4. ...
  5. >
  6. ...
  7. </application>
  8. </manifest>

当然,你可能有自己的Application并且已经配置好,如下:

  1. <manifest>
  2. <application
  3. android:name="com.example.MyOwnApplication"
  4. ...
  5. >
  6. ...
  7. </application>
  8. </manifest>

没关系,只需要将MyOwnApplication由原来的继承Application类改成继承LitePalApplication类就可以,如下:

  1. public class MyOwnApplication extends LitePalApplication {
  2. ...
  3. }

如果你的MyOwnApplication必须继承另外的Application类,如AnotherApplication类,那么你可以直接调用LitePalApplication.initialize(context)而无需继承LiteApplication类,如下:

  1. public class MyOwnApplication extends AnotherApplication {
  2.  
  3. @Override
  4. public void onCreate() {
  5. super.onCreate();
  6. LitePalApplication.initialize(this);
  7. }
  8. ...
  9. }

LitePalApplication.initialize(context)的调用原则是尽可能早,比如合适的调用位置是在Application的onCreate()里调用。调用时传递的参数是Application的context,不要使用任何activity或service的实例作为参数,否则可能发生内存泄漏。

开始LitePal体验之旅

1. 创建表格

首先定义各种model,比如有两个model:Album和Song,定义如下:

  1. public class Album extends DataSupport {
  2.  
  3. @Column(unique = true, defaultValue = "unknown")
  4. private String name;
  5.  
  6. private float price;
  7.  
  8. private List<Song> songs = new ArrayList<Song>();
  9.  
  10. // generated getters and setters.
  11. ...
  12. }
  1. public class Song extends DataSupport {
  2.  
  3. @Column(nullable = false)
  4. private String name;
  5.  
  6. private int duration;
  7.  
  8. @Column(ignore = true)
  9. private String uselessField;
  10.  
  11. private Album album;
  12.  
  13. // generated getters and setters.
  14. ...
  15. }

将这两个model添加到litepal.xml的映射表中,如下:

  1.  
  1. <list>
  2. <mapping class="org.litepal.litepalsample.model.Album"></mapping>
  3. <mapping class="org.litepal.litepalsample.model.Song"></mapping>
  4. </list>

一旦操作数据库时,数据库表格将自动生成。比如使用以下代码获取SQLiteDatabase时,

  1. SQLiteDatabase db = Connector.getDatabase();

将自动生成album和song两张数据库表格,如下:

  1. CREATE TABLE album (
  2. id integer primary key autoincrement,
  3. name text unique default 'unknown',
  4. price real
  5. );
  6.  
  7. CREATE TABLE song (
  8. id integer primary key autoincrement,
  9. name text not null,
  10. duration integer,
  11. album_id integer
  12. );

2. 升级表格

在LitePal中实现升级表格非常容易。

  1. public class Album extends DataSupport {
  2.  
  3. @Column(unique = true, defaultValue = "unknown")
  4. private String name;
  5.  
  6. @Column(ignore = true)
  7. private float price;
  8.  
  9. private Date releaseDate;
  10.  
  11. private List<Song> songs = new ArrayList<Song>();
  12.  
  13. // generated getters and setters.
  14. ...
  15. }

上述代码中,添加了releaseDate并且price标注为ignore。

  1. <!--
  2. Define the version of your database. Each time you want
  3. to upgrade your database, the version tag would helps.
  4. Modify the models you defined in the mapping tag, and just
  5. make the version value plus one, the upgrade of database
  6. will be processed automaticly without concern.
  7. For example:
  8. <version value="1" ></version>
  9. -->
  10. <version value="2" ></version>

只需要在litepal.xml中升级版本号,那么下次操作数据库时表格将自动升级:ablum表中添加了releasedate列,删除了price列,其他列的数据原封不动。

以下一些升级情况LitePal无法处理并且被升级表格里的所有数据将被清空:

  • 添加了一个标注为 unique = true 的属性;
  • 修改某个属性的标注为 unique = true;
  • 修改某个属性的标注为 nullable = false;

以上情况会导致数据丢失,要格外注意。

3. 保存数据

每一个继承DataSupport类的model都有save()方法。

  1. Album album = new Album();
  2. album.setName("album");
  3. album.setPrice(10.99f);
  4. album.save();
  5. Song song1 = new Song();
  6. song1.setName("song1");
  7. song1.setDuration(320);
  8. song1.setAlbum(album);
  9. song1.save();
  10. Song song2 = new Song();
  11. song2.setName("song2");;
  12. song2.setDuration(356);
  13. song2.setAlbum(album);
  14. song2.save();

以上代码实现将album, song1和song2插入到数据库中并建议关联。

4. 更新数据

继承DataSupport类的每一个model都有update()和updateAll()方法。update()可更新指定id的单条记录,如下:

  1. Album albumToUpdate = new Album();
  2. albumToUpdate.setPrice(20.99f); // raise the price
  3. albumToUpdate.update(id);

updateAll()可同时更新满足一定条件的多条记录,如下:

  1. Album albumToUpdate = new Album();
  2. albumToUpdate.setPrice(20.99f); // raise the price
  3. albumToUpdate.updateAll("name = ?", "album");

5. 删除数据

调用DataSupport的静态方法delete()可删除指定id的单条记录:

  1. DataSupport.delete(Song.class, id);

也可调用静态方法deleteAll()删除多条记录:

  1. DataSupport.deleteAll(Song.class, "duration > ?" , "350");

6. 查询数据

查询song表中指定id的单条记录:

  1. Song song = DataSupport.find(Song.class, id);

查询song表中的所有记录:

  1. List<Song> allSongs = DataSupport.findAll(Song.class);

构建复杂的集群查询:

  1. List<Song> songs = DataSupport.where("name like ?", "song%").order("duration").find(Song.class);

Android LitePal介绍与使用说明的更多相关文章

  1. android Animation介绍

    Animation介绍: 在Android SDK介绍了2种Animation模式: 1. Tween Animation:间动画,通过对场景里的对象不断做图像变换(平移.缩放.旋转)产生动画效果,即 ...

  2. android AsyncTask介绍(转)

    android AsyncTask介绍 AsyncTask和Handler对比 1 ) AsyncTask实现的原理,和适用的优缺点 AsyncTask,是android提供的轻量级的异步类,可以直接 ...

  3. Android monkey介绍

    Android monkey介绍 原文地址 1 简略 monkey是android下自动化测试比较重要的的一个工具,该工具可以运行在host端或者设备(模拟器或真实设备).它会向系统发送随机事件流(即 ...

  4. [Learn Android Studio 汉化教程]第一章 : Android Studio 介绍

    注:为了看上去比较清晰这里只转载了中文 原地址:  [Learn Android Studio 汉化教程]第一章 : Android Studio 介绍 本章将引导您完成安装和设置开发环境,然后你就可 ...

  5. 【转】Android bluetooth介绍(三): 蓝牙扫描(scan)设备分析

    原文网址:http://blog.csdn.net/xubin341719/article/details/38584469 关键词:蓝牙blueZ  A2DP.SINK.sink_connect.s ...

  6. Android bluetooth介绍(四): a2dp connect流程分析

    关键词:蓝牙blueZ  A2DP.SINK.sink_connect.sink_disconnect.sink_suspend.sink_resume.sink_is_connected.sink_ ...

  7. android动画介绍之 自己定义Animation动画实现qq抖一抖效果

    昨天我们介绍了Animation的基本使用方法.小伙伴们了解的怎么样了?假设还没有了解过Animation的小伙伴能够看看这篇博客 android动画介绍--Animation 实现loading动画 ...

  8. android动画介绍之 自定义Animation动画实现qq抖一抖效果

    昨天我们介绍了Animation的基本用法.小伙伴们了解的怎么样了?如果还没有了解过Animation的小伙伴可以看看这篇博客 android动画介绍--Animation 实现loading动画效果 ...

  9. android动画介绍--Animation 实现loading动画效果

    Animation的使用方法并不难.这里简单的介绍一下使用方法. 先看效果图: 效果还是不错的吧. 下面来看看使用方法. 动画效果是通过Animation来实现的,一共有四种,分别为: AlphaAn ...

随机推荐

  1. 二、mysql安装详解

    step1:打开下载的mysql安装文件(mysql-5.5.36-win32.msi),双击运行,如下图: step2:点击“Next”按钮继续,如下图: step3:点击“勾选”,点击“Next” ...

  2. struts2 ValueStack的set方法与setValue方法的区别

    struts2中 ValueStack的set方法与setValue方法的区别呢? 示例代码: ActionContext.getContext().getValueStack().setValue( ...

  3. Ubuntu 安装 texlive

    下载网站: http://tug.org/texlive/acquire-netinstall.html 此处解释texlive配置PATH gedit ~/.bashrc 在文件最后添加以下内容, ...

  4. K个联通块

    题意: 有一张无重边的无向图, 求有多少个边集,使得删掉边集里的边后,图里恰好有K个联通块. 解法: 考虑dp,$h(i,S)$表示有$i$个联通块,点集为$S$的图的个数,$g(S)$表示点集为S的 ...

  5. 1.5 Hive初步使用和安装MySQL

    一.HQL初步试用 1.创建一个student表 #创建一个student表 hive> create table student(id int, name string) ROW FORMAT ...

  6. c++函数模板二栈实现

    1 没有使用模板的栈实现 #include <iostream> #include <string> using namespace std; class Stack { pu ...

  7. c语言编译器内置宏

    注:转自http://www.cnblogs.com/lixiaohui-ambition/archive/2012/08/21/2649052.html  感谢分享 前言: 我们在写程序的时候,总是 ...

  8. 2019ICPC西安邀请赛 - B. Product - 数论

    打印的时候麻烦把:https://blog.csdn.net/skywalkert/article/details/50500009这个打印下来. 求\(\prod\limits_{i=1}^{n} ...

  9. CodeForces 644B【模拟】

    题意: 查询数 和 最大的队列容量+1: 按时间顺序 ti代表,第i个出线的时间: di代表,第i个需要处理的时间: 对于第i个输出他所需要的时间完成,或者拒绝进入输出-1: 思路: 真是MDZZ了, ...

  10. 聊聊IT行业加班的问题

    IT行业(包括互联网行业)是快速发展的行业,有时候一家公司同时可能要开发多个项目,并发进行,在公司开发人员相对固定的情况下,要想在指定的时间内完成项目谈何容易. 项目多.任务重.需求的不明确.技术难关 ...