Android开发中常用的数据库有5个:
1.OrmLite
OrmLite 不是 Android 平台专用的ORM框架,它是Java ORM。支持JDBC连接,Spring以及Android平台。语法中广泛使用了注解(Annotation)。2.SugarORM
SugarORM 是 Android 平台专用ORM。提供简单易学的APIs。可以很容易的处理1对1和1对多的关系型数据,并通过3个函数save(), delete() 和 find() (或者 findById()) 来简化CRUD基本操作。3.GreenDAO
当性能很重要时(数据访问频繁),GreenDao是一个很快的解决方案,它能够支持数千条记录的CRUD每秒,和OrmLite相比,GreenDAO要快几乎4.5倍。(准确数据请自行benchmark)。GreenDAO小于100KB,所以对于应用程序APK的大小影响很小。
4.Active Android
Active Record(活动目录)是Yii、Rails等框架中对ORM实现的典型命名方式。Active Android 帮助你以面向对象的方式来操作SQLite。在你的项目中包含Active Android,你需要在项目的 /libs 目录下添加一个jar文件。可以从Github中获取源代码并使用Maven进行编辑。
5.Realm
Realm 是一个将可以使用的Android ORM,基于C++编写,直接运行在你的设备硬件上(不需要被解释),因此运行很快。它同时是开源跨平台的,iOS的代码可以在GitHub找到,你还可以找到Objective C以及Swift编写的Realm使用实例。相比SQLite,Realm更快并且具有很多现代数据库的特性,比如支持JSON,流式api,数据变更通知,以及加密支持,这些都为安卓开发者带来了方便。
Ream提供了五种编程方式的实现。分别是Java,Objective C,Swift,React-Native,tamarin。在这里我着重介绍在Android中的使用。
大家可以直接看官方的文档,我也是在这基础的上讲解:realm使用
快速入门:
运行环境
- 目前我们还不支持 Android 以外的 Java 环境;
- Android Studio >= 1.5.1 ;
- 较新的 Android SDK 版本;
- JDK 版本 >=7;
- 我们支持 Android API 9 以上的所有版本(Android 2.3 Gingerbread 及以上)。 构建依赖关系
第一步:在项目的 build.gradle 文件中添加如下 class path 依赖。
1
2
3
4
5
6
7
8
|
<code>buildscript {
repositories {
jcenter()
}
dependencies {
classpath
"io.realm:realm-gradle-plugin:1.0.0"
}
}</code>
|
项目的
build.gradle文件在如下位置:
第二步:在 app 的 build.gradle 文件中应用
realm-android插件。
1
|
<code><code><code><code>apply plugin:
'realm-android'
</code></code></code></code>
|
app的
build.gradle文件在如下位置:
如果读者使用的是maven构建项目,官方也有说明,这里我就不列出来了。
说到这里,我们已经迫不及待的想尝试下了。
1,application里面初始化
1
2
3
4
5
6
7
8
9
10
11
12
|
<code><code><code><code><code>
public
class
RealmApplication
extends
Application {
@Override
public
void
onCreate() {
super
.onCreate();
init();
}
private
void
init() {
RealmConfiguration realmConfiguration =
new
RealmConfiguration.Builder(
this
).build();
Realm.setDefaultConfiguration(realmConfiguration);
}
}</code></code></code></code></code>
|
2,创建工具管理类,获取Realm实例
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
|
<code><code><code><code><code>
public
class
RealmUtils {
private
static
RealmUtils instance;
public
final
Context mContext;
private
String realmName =
"realm_demo.realm"
;
public
RealmUtils(Context mContext) {
this
.mContext = mContext;
}
public
static
RealmUtils getInstance(Context context){
if
(instance ==
null
) {
synchronized
(RealmUtils.
class
) {
if
(instance ==
null
) {
instance =
new
RealmUtils(context);
}
}
}
return
instance;
}
public
Realm getRealm(){
Realm realm =Realm.getInstance(
new
RealmConfiguration.Builder(mContext).name(realmName).build());
return
realm;
}
}</code></code></code></code></code>
|
3,创建一个realmObject对象,存储object
例如,我们现在需要存储一个人(Person)对象,注意,这里的成员属性为了realm的序列化,都写成私有的
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
<code><code><code><code><code>
public
class
Person
extends
RealmObject {
@PrimaryKey
private
String code;
//编号
private
String name;
//姓名
private
int
age;
//年龄
public
Person() {
}
public
Person(
int
age, String code, String name) {
this
.age = age;
this
.code = code;
this
.name = name;
}
public
String getCode() {
return
code;
}
public
void
setCode(String code) {
this
.code = code;
}
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
public
int
getAge() {
return
age;
}
public
void
setAge(
int
age) {
this
.age = age;
}
@Override
public
String toString() {
return
"Person{"
+
"code='"
+ code + '\
''
+
", name='"
+ name + '\
''
+
", age="
+ age +
'}'
;
}
}</code></code></code></code></code>
|
4,定义几个CRUD的方法,供其它类实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<code><code><code><code><code>
public
interface
PersonDao {
//插入
void
insert(Person person)
throws
Exception;
//查询
List<person> getAllPerson()
throws
Exception;
//更新
Person updatePerson(Person person)
throws
Exception;
//删除
void
deletePerson(String code)
throws
Exception;
// 异步插入
void
insertPersonAsync(Person person)
throws
Exception;
}</person></code></code></code></code></code>
|
5,对象的具体实现
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
<code><code><code><code><code>
public
class
PersonDaoImpl
implements
PersonDao {
private
Context context;
private
Realm mRealm;
public
PersonDaoImpl(Context context){
mRealm = RealmUtils.getInstance(context).getRealm();
}
@Override
public
void
insert(Person person)
throws
Exception {
mRealm.beginTransaction();
Person person1 = mRealm.copyToRealm(person);
mRealm.commitTransaction();
mRealm.close();
}
@Override
public
List<person> getAllPerson()
throws
Exception {
List<person> mlist =
null
;
mlist = mRealm.where(Person.
class
).findAll();
mRealm.close();
return
mlist;
}
@Override
public
Person updatePerson(Person person)
throws
Exception {
mRealm.beginTransaction();
Person person1 = mRealm.copyToRealmOrUpdate(person);
mRealm.commitTransaction();
mRealm.close();
return
person1;
}
@Override
public
void
deletePerson(String code)
throws
Exception {
Person person = mRealm.where(Person.
class
).equalTo(
"code"
,code).findFirst();
mRealm.beginTransaction();
person.deleteFromRealm();
mRealm.commitTransaction();
}
@Override
public
void
insertPersonAsync(
final
Person person)
throws
Exception {
//一个Realm只能在同一个线程中访问,在子线程中进行数据库操作必须重新获取Realm对象:
mRealm.executeTransaction(
new
Realm.Transaction() {
@Override
public
void
execute(Realm realm) {
realm.beginTransaction();
Person person1 = realm.copyToRealm(person);
realm.commitTransaction();
realm.close();
//并且要记得在离开线程时要关闭 realm.close();
}
});
//关闭Realm对象
mRealm.close();
}
}</person></person></code></code></code></code></code>
|
6,测试
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
30
31
32
33
34
35
|
<code><code><code><code><code>
public
class
MainActivity
extends
AppCompatActivity {
private
Realm mRealm;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private
void
init() {
Person person =
new
Person();
person.setName(
"测试"
);
person.setAge(
28
);
person.setCode(
"xxxx"
);
PersonDao dao =
new
PersonDaoImpl(
this
);
try
{
//增加
dao.insert(person);
//查询全部
dao.getAllPerson();
//指定code删除
dao.deletePerson(
"xxxx"
);
//更新
dao.updatePerson(person);
}
catch
(Exception e) {
e.printStackTrace();
}
}
}</code></code></code></code></code>
|
其实这和以前的用法差不多的,有点在于,realm去帮我们管理这个数据库,并且在安全上我这还没有讲到,这里只讲了基本用法,大家可以去看看官方的文档说明: