Android ORM SQL Top 5
If you are developing an Android application, you will likely need to store data somewhere. You may choose a Cloud service (in which case, using a SyncAdapter would be a good idea), or to store your data in the embedded SQLite database. If you take the second option, you will have to decide between writing SQL queries, using a Content Provider (useful if you want to share your data with other apps), or using an ORM.
In this article, I will discuss some of the Android ORMs you may consider using in your application.
OrmLite
OrmLite is the first Android ORM that comes to my mind. However OrmLite is not an Android ORM, it’s a Java ORM with SQL databases support. It can be used anywhere Java is used, such as JDBC connections, Spring, and also Android.
It makes heavy usage of annotations, such as @DatabaseTable for each class that defines a table, or@DatabaseField for each field in the class.
A simple example of using OrmLite to define a table would be something like this:
|
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
|
@DatabaseTable(tableName = "users")public class User { @DatabaseField(id = true) private String username; @DatabaseField private String password; public User() { // ORMLite needs a no-arg constructor } public User(String username, String password) { this.username = username; this.password = password; } // Implementing getter and setter methods public String getUserame() { return this.username; } public void setName(String username) { this.username = username; } public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; }} |
OrmLite for Android is open source and you can find it on GitHub. For more information read its official documentation here.
SugarORM
SugarORM is an ORM built only for Android. It comes with an API which is both simple to learn and simple to remember. It creates necessary tables itself, gives you a simple methods of creating one-to-one and one-to-many relationships, and also simplifies CRUD by using only 3 functions, save(), delete() and find()(or findById()).
Configure your application to use SugarORM by adding these four meta-data tags to your appsAndroidManifest.xml:
|
1
2
3
4
|
<meta-data android:name="DATABASE" android:value="my_database.db" /><meta-data android:name="VERSION" android:value="1" /><meta-data android:name="QUERY_LOG" android:value="true" /><meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.my-domain" /> |
Now you may use this ORM by extending it in the classes you need to make into tables, like this:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class User extends SugarRecord<User> { String username; String password; int age; @Ignore String bio; //this will be ignored by SugarORM public User() { } public User(String username, String password,int age){ this.username = username; this.password = password; this.age = age; }} |
So adding a new user would be:
|
1
2
|
User johndoe = new User(getContext(),"john.doe","secret",19);johndoe.save(); //stores the new user into the database |
Deleting all the users of age 19 would be:
|
1
2
3
4
|
List<User> nineteens = User.find(User.class,"age = ?",new int[]{19});foreach(user in nineteens) { user.delete();} |
For more, read SugarORM’s online documentation.
GreenDAO
When it comes to performance, ‘fast’ and GreenDAO are synonymous. As stated on its website, “most entities can be inserted, updated and loaded at rates of several thousand entities per second”. If it wasn’t that good,these apps wouldn’t be using it. Compared to OrmLite, it is almost 4.5 times faster.

greenDAO vs OrmLite
Speaking of size, it is smaller than 100kb, so doesn’t affect APK size very much.
Follow this tutorial, which uses Android Studio to show the usage of GreenDAO in an Android application. You can view the GreenDAO source code on GitHub, and read the GreenDAO official documentation.
Active Android
Much like other ORMs, ActiveAndroid helps you store and retrieve records from SQLite without writing SQL queries.
Including ActiveAndroid in your project involves adding a jar file into the /libs folder of your Android project. As stated in the Getting started guide, you can clone the source code from GitHub and compile it using Maven. After including it, you should add these meta-data tags into your app’sAndroidManifest.xml:
|
1
2
|
<meta-data android:name="AA_DB_NAME" android:value="my_database.db" /><meta-data android:name="AA_DB_VERSION" android:value="1" /> |
After adding these tags, you can call ActiveAndroid.initialize() in your activity like this:
|
1
2
3
4
5
6
7
8
9
|
public class MyActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActiveAndroid.initialize(this); //rest of the app }} |
Now that the application is configured to use ActiveAndroid, you may create Models as Java classes by usingAnnotations:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@Table(name = "User")public class User extends Model { @Column(name = "username") public String username; @Column(name = "password") public String password; public User() { super(); } public User(String username,String password) { super(); this.username = username; this.password = password; }} |
This is a simple example of ActiveAndroid usage. The documentation will help you understand the usage of ActiveAndroid ORM further.
Realm
Finally Realm is a ‘yet-to-come’ ORM for Android which currently only exists. It is built on C++, and runs directly on your hardware (not interpreted) which makes it really fast. The code for iOS is open source, and you can find it on GitHub.
On the website you will find some use cases of Realm in both Objective-C and Swift, and also a Registration form to get the latest news for the Android version.
Final words
These are not the only Android ORMs on the market. Other examples are Androrm and ORMDroid.
SQL knowledge is a skill that every developer should have, but writing SQL queries is boring, especially when there are so many ORMs out there. When they make your job simpler, why not use them in the first place?
How about you? What Android ORM do you use? Comment your choice below
Android ORM SQL Top 5的更多相关文章
- 最好的5个Android ORM框架
在开发Android应用时,保存数据有这么几个方式, 一个是本地保存,一个是放在后台(提供API接口),还有一个是放在开放云服务上(如 SyncAdapter 会是一个不错的选择). 对于第一种方式, ...
- 简单实用的Android ORM框架TigerDB
TigerDB是一个简单的Android ORM框架,它能让你一句话实现数据库的增删改查,同时支持实体对象的持久化和自动映射,同时你也不必关心表结构的变化,因为它会自动检测新增字段来更新你的表结构. ...
- 最受欢迎的5个Android ORM框架
在开发Android应用时,保存数据有这么几个方式, 一个是本地保存,一个是放在后台(提供API接口),还有一个是放在开放云服务上(如 SyncAdapter 会是一个不错的选择). 对于第一种方式, ...
- SQL TOP 子句、SQL LIKE 操作符、SQL 通配符
TOP 子句 TOP 子句用于规定要返回的记录的数目. 对于拥有数千条记录的大型表来说,TOP 子句是非常有用的. 注释:并非所有的数据库系统都支持 TOP 子句. SQL Server 的语法: S ...
- Android ORM应用开发框架KJFrameForAndroid使用详解
本文将为大家介绍一款Android ORM应用开发框架KJFrameForAndroid,很多时候我们也叫它KJLibrary. KJFrameForAndroid简介 KJFrameForAndro ...
- 推荐的Android ORM框架
1. OrmLite OrmLite 不是 Android 平台专用的ORM框架,它是Java ORM.支持JDBC连接,Spring以及Android平台.语法中广泛使用了注解(Annotation ...
- Android 连接 SQL Server (jtds方式)——上
本文将介绍开发Android程序,连接SQL Server,通过第三方包jtds的方式. 如果你有同样的需求,请跟着做一遍,博主将以最详细的方式,进行介绍. 首先说明,Java.Android连接SQ ...
- SQL TOP分页
SQL TOP分页 2010-11-12 16:35:29| 分类: SQL | 标签: |字号大中小 订阅 1.分页方案一:(利用Not In和SELECT TOP分页) 语句形式: ...
- SQL TOP 子句
TOP 子句 TOP 子句用于规定要返回的记录的数目. 对于拥有数千条记录的大型表来说,TOP 子句是非常有用的. 注释:并非所有的数据库系统都支持 TOP 子句. SQL Server 的语法: S ...
随机推荐
- asp.net 一个简单的登录控制
如果说一个网站需要用户登录后才能浏览,那么用户登录控制就不可避免.但是对于几百个以上的页面,不可能每个页面都做一次登录验证.因此,这需要在母版页中进行登录控制,这样就可以使得每一个使用这个母版页的子页 ...
- .Net 缓存依赖详解
缓存命名空间的讲解流程 16.1 System.Web.Caching简介 本节从缓存命名空间的总体简介和组成结构入手,从整体上对System.Web.Caching进行概述. 16.1.1 Sy ...
- SSAS中CUBE行权限数据级权限控制
去年做了一个数据仓库的项目,其中涉及到了CUBE数据级权限的控制.在网上找这方面的资料,找到一个[BI] 通用数据级权限控制解决方案的实现(二):Cube中的角色设置与数据级权限控制.根据这个大牛的思 ...
- MRP工作台任务下达之x组织屏蔽全部发放功能
应用 Oracle Manufacturing Planning 层 Level Function 函数名 Funcgtion Name MRPFPPWB-390 表单名 Form Name MR ...
- C#中的多线程-入门
概述与概念C#支持通过多线程并行地执行代码,一个线程有它独立的执行路径,能够与其它的线程同时地运行.一个C#程序开始于一个单线程,这个单线程是被CLR和操作系统(也称为“主线程”)自动创建的,并具有多 ...
- java总结
JUC概况 以下是Java JUC包的主体结构: ? Atomic : AtomicInteger ? Locks : Lock, Condition, ReadWriteLock ? Collect ...
- C++容器在遍历时的删除问题
容器是非常便捷常用的,经常用容器来存储多条数据,然后对数据进行增删查改. 有时要在遍历的同时删除一条数据,但是这样删除的时候程序会导致程序崩溃. 这个问题在GCC 中不会出现,而在VS2008,VS2 ...
- Ubuntu 12.04 wireless networks : devices not ready (firmware missing)解决办法
今天装了Ubuntu12.04之后,发现无线不能用. 用iwconfig查看,wlan0 项后面有内容,但是在本该显示无线列表的地方显示的是 “ wireless networks : devices ...
- sublime前端编辑器入门与个人使用经验分享
Sublime Text(以下简称sublime)是一款很好用的代码编辑器,小巧且很灵敏,几乎可以编写大部分主流的计算机语言代码,更是堪称前端代码编辑神器. 你百度一下会发现许多sublime的安装和 ...
- CSS中字体尺寸总结
下面是我总结的css中关于字体尺寸的知识,欢迎高手拍砖! 前端开发过程中,我们经常会遇到设置某个div固定显示几行文本:这时我们需要精确计算每个字号字体的宽度和高度. 下面是w3school中描述的尺 ...