LitePal数据库插件

1.编辑你的build.gradle文件并添加下面的依赖库:

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

2.配置litepal.xml
在项目的assets文件夹中创建一个文件,并将其命名为litepal.xml,创建步骤为app–src–main–new–directory,命名为assets,在assets文件夹下创建litepal.xml(assets–new–File)。然后将以下代码复制到其中。

<?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 automatically for you.
        For example:    
        <dbname value="demo" />
    -->
    <dbname value="demo" />

    <!--
        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 automatically without concern.
            For example:    
        <version value="1" />
    -->
    <version value="1" />

    <!--
        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 class="com.test.model.Magazine" />
        </list>
    -->
    <list>
    </list>

    <!--
        Define where the .db file should be. "internal" means the .db file
        will be stored in the database folder of internal storage which no
        one can access. "external" means the .db file will be stored in the
        path to the directory on the primary external storage device where
        the application can place persistent files it owns which everyone
        can access. "internal" will act as default.
        For example:
        <storage value="external" />
    -->

</litepal>

3.配置LitePalApplication
你不想一直传递Context参数。为了简化API,只需在AndroidManifest.xml中配置LitePalApplication,如下所示:

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

当然,你可能有自己的应用程序,并且已经在这里配置好了,只需在您自己的应用程序中调用LitePal.initialize(context):

public class MyOwnApplication extends Application {

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

到这里就完成了设置,开始使用
1.创建表格
首先定义模型。例如,你有两个模型,专辑和歌曲。模型可以定义如下:

public class Album extends DataSupport {

    @Column(unique = true, defaultValue = "unknown")
    private String name;

    private float price;

    private byte[] cover;

    private List<Song> songs = new ArrayList<Song>();

    // generated getters and setters.
    ...
}
public class Song extends DataSupport {

    @Column(nullable = false)
    private String name;

    private int duration;

    @Column(ignore = true)
    private String uselessField;

    private Album album;

    // generated getters and setters.
    ...
}

然后将这些模型添加到litepal.xml中的映射列表中:

<list>
    <mapping class="org.litepal.litepalsample.model.Album" />
    <mapping class="org.litepal.litepalsample.model.Song" />
</list>

好!这些表格将在您下次操作数据库时生成。例如,使用以下代码获取SQLiteDatabase:

SQLiteDatabase db = LitePal.getDatabase();

LitePal中的升级表非常简单。无论如何只要修改你的模型:

public class Album extends DataSupport {

    @Column(unique = true, defaultValue = "unknown")
    private String name;

    @Column(ignore = true)
    private float price;

    private byte[] cover;

    private Date releaseDate;

    private List<Song> songs = new ArrayList<Song>();

    // generated getters and setters.
    ...
}

然后在litepal.xml中增加版本号:

<version value="2" ></version>

下面可以进行增删改查
增:

String name = albumEdit.getText().toString();
float price = 109.8f;
Album album = new Album();
album.setName(name);
album.setPrice(price);
album.save();

删:

Album album1 = DataSupport.find(Album.class,1);
album1.setName("White");
album1.save();

改:

int row = DataSupport.deleteAll(Album.class,"id<?","3");

查:

List<Album> albumList = DataSupport.where("name like ?","a%").order("name").find(Album.class);
for (Album a:
     albumList) {
    Log.e("MAIN",a.getName()+"**********");
     }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值