LitePal: Android开源数据库框架(一)

LitePal 是一个开源的Android数据库框架,它让Android的数据库开发工作变得更简单,使用逻辑变得更清晰、简洁。通过LitePal可以完成几乎所有的Android Sqlite的功能。以下是作者对它的介绍:

  • LitePal is an Open Source Android library that allows developers to use SQLite database extremely easy. You can finish most of the database operations without writing even a SQL statement, including create or upgrade tables, crud operations, aggregate functions, etc. The setup of LitePal is quite simple as well, you can integrate it into your project in less than 5 minutes.
  • Experience the magic right now and have fun!

LitePal在Github上的地址:LitePalFramework/LitePal

它的用法同样非常简单,本文将介绍LitePal的基本使用。

开始使用

1、集成LitePal到你的工程中

Eclipse

Eclipse下可直接到Github上下载最新版本的jar包放在libs下并添加到build path即可。

Android Studio

第一种方式:编辑build.gradle文件,在dependencies中添加引用语句:

dependencies {
    compile 'org.litepal.android:core:1.2.1'
}

第二种方式:同样,也可以像eclipse一样,把jar包放在libs下并确保build.gradle的dependencies中包含如下语句:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

第三种方式:
1、到Github下载LitePal的源码到本地,通过File->New->Import Module...的方式引用源码为Project的一个Module:
how-import-litepal-module
2、在build.gradle中添加dependencies语句引用litepal:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':litepal')
}

以上三种方式,根据项目需要,任选一种,都可以集成LitePal到你的工程中。

2、有一点点配置

LitePal的配置极其简单。一个文件,加一行代码,就能轻松搞定。

1、一个文件: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:配置数据库的版本号
list:配置每张表对应的实体类,后面会详细说。

2、一行代码

最简单的一种方式:
AndroidManifest.xml中按如下方式配置:

<manifest>
    <application
        android:name="org.litepal.LitePalApplication"
        ...
    >
    ...
    </application>
</manifest>

当然如果你的工程中已经使用的自定义的Application,像这样:

<manifest>
    <application
        android:name="com.example.MyOwnApplication"
        ...
    >
    ...
    </application>
</manifest>

也好办,换个地方,换一行代码,像这样:

public class MyOwnApplication extends LitePalApplication {
    ...
}

但是你自定义的Application有可能已经继承了其它的Application,那就再换个地方,再换一行代码:

public class MyOwnApplication extends AnotherApplication {

    @Override
    public void onCreate() {
        super.onCreate();
        LitePalApplication.initialize(this);
    }
    ...
}

保证尽早调用LitePalApplication.initialize(Context context);并且保证以ApplicationContext作为参数调用。不要使用Activity、Service的Context。

Well done.一个文件,一行代码,你的数据库就已经准备就绪了,现在你可以尽情使用LitePal了。

posted @ 2015-07-09 15:34  Taylor & Code  阅读(878)  评论(0编辑  收藏  举报