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 aSyncAdapterwould 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 anORM.

In this article, I will discuss some of the Android ORMs you may consider using in your application.

OrmLite

OrmLiteis 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 asJDBCconnections,Spring, and also Android.

It makes heavy usage of annotations, such as@DatabaseTablefor each class that defines a table, or@DatabaseFieldfor 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 onGitHub. For more information readits official documentation here.

SugarORM

SugarORMis 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()andfind()(orfindById()).

Configure your application to use SugarORM by adding these fourmeta-datatags 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’sonline documentation.

GreenDAO

When it comes to performance, ‘fast’ andGreenDAOare synonymous. As stated on its website,“most entities can be inserted, updated and loaded at rates ofseveral thousand entities per second”. If it wasn’t that good,these appswouldn’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.

Followthis tutorial, which uses Android Studio to show the usage of GreenDAO in an Android application. You can view the GreenDAO source code onGitHub, and read theGreenDAO official documentation.

Active Android

Much like other ORMs,ActiveAndroidhelps you store and retrieve records from SQLite without writing SQL queries.

Including ActiveAndroid in your project involves adding ajarfile into the/libsfolder of your Android project. As stated in theGetting startedguide, you can clone the source code fromGitHuband compile it usingMaven. After including it, you should add thesemeta-datatags 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 callActiveAndroid.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. Thedocumentationwill help you understand the usage of ActiveAndroid ORM further.

Realm

FinallyRealmis 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 onGitHub.

On the website you will find some use cases of Realm in both Objective-C and Swift, and also aRegistration formto get the latest news for the Android version.

Final words

These are not the only Android ORMs on the market. Other examples areAndrormandORMDroid.

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

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. android view的setVisibility方法值的意
  2. android adb录屏命令
  3. android 流量的统计
  4. 国内阉割版安卓手机无法使用google maps
  5. Android退出应用的确认
  6. Android(安卓)判断网络状态
  7. Android(安卓)App性能之--cpu占用率
  8. volley Demo
  9. IDA动态调试Android进程的配置步骤
  10. Android中的Matrix类介绍