Git Product home page Git Product logo

litepaldemo's Introduction

LitePalDemo

郭大神写的数据库框架LitePal的基本使用demo,LitePal地址 https://github.com/LitePalFramework/LitePal

效果图

00.gif

快速配置

1.引入jar包

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

2.配置litepal.xml

assets 文件夹下添加 litepal.xml

<?xml version="1.0" encoding="utf-8"?>
<litepal>
    <!--数据库名字-->
    <dbname value="user" />
    <!--数据库版本-->
    <version value="1" />
    <list>
        <!--根据model映射创建表-->
        <mapping class="com.wuxiaosu.litepaldemo.model.User" />
    </list>
    <!--将数据库文件存放在sd卡(/sdcard/Android/data/<package name>/files/databases),调试时使用(internal,external)-->
    <!--<storage value="external" />-->
</litepal>

3.配置LitePalApplication

酱紫:

  • 方式一
<manifest>
    <application android:name="org.litepal.LitePalApplication"
        ...
        >
        ...
    </application>
</manifest>

或者:

  • 方式二
<manifest>
    <application android:name="org.litepal.MyOwnApplication"
        ...
        >
        ...
    </application>
</manifest>

这种方式需要继承 LitePalApplication

public class MyOwnApplication extends LitePalApplication {  
    ...  
}

或者:

  • 方式三
public class MyOwnApplication extends AnotherApplication {

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

开始使用

1.建表

首先定义model(需要继承 DataSupport ),酱紫:

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>
    <mapping class="org.litepal.litepalsample.model.Song"></mapping>
</list>

表会在下次数据库操作时自动创建,也可以通过以下方式获取SQLiteDatabase实例:

SQLiteDatabase db = LitePal.getDatabase();

2.更新表

直接根据需要修改model(增、删字段)就好了:

public class Album extends DataSupport {

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

    //ignore
    @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"/>

3.保存数据

继承DataSupport可以直接使用 save() 方法,酱紫:

Album album = new Album();
album.setName("album");
album.setPrice(10.99f);
album.setCover(getCoverImageBytes());
album.save();

或者:

List<Album> newsList;  
...  
DataSupport.saveAll(newsList); 

4.修改数据

save() 方法修改通过 find() 取出的数据:

Album albumToUpdate = DataSupport.find(Album.class, 1);
albumToUpdate.setPrice(20.99f); // raise the price
albumToUpdate.save();

或者根据id使用 update() 方法修改数据:

Album albumToUpdate = new Album();
albumToUpdate.setPrice(20.99f); // raise the price
albumToUpdate.update(id);

或者通过条件使用 updateAll() 方法修改多条数据:

Album albumToUpdate = new Album();
albumToUpdate.setPrice(20.99f); // raise the price
albumToUpdate.updateAll("name = ?", "album");

5.删除数据

通过id使用 DataSupport.delete() 方法删除单条记录:

DataSupport.delete(Song.class, id);

或者通过条件使用 DataSupport.deleteAll() 方法删除多条记录:

DataSupport.deleteAll(Song.class, "duration > ?" , "350");

如果对象经过序列化(save()),也可以这样删除:

News news = new News();  
news.setTitle("这是一条新闻标题");  
news.setContent("这是一条新闻内容");  
news.save();  
...  
news.delete(); 

6.查询数据

通过id查询单条数据:

Song song = DataSupport.find(Song.class, id);

通过id查询多条数据:

List<Song> newsList = DataSupport.findAll(Song.class, 1, 3, 5, 7); 

也可以这样写:

long[] ids = new long[] { 1, 3, 5, 7 };  
List<Song> newsList = DataSupport.findAll(Song.class, ids); 

查询第一条数据

DataSupport.findFirst(Song.class); 

查询最后一条数据

DataSupport.findLast(Song.class); 

查询某张表下的所有数据:

List<Song> allSongs = DataSupport.findAll(Song.class);

通过条件查询:

List<Song> songs = DataSupport.select("title", "content")
        .where("name like ?", "song%")
        .order("duration")
        .limit(10)
        .offset(10)
        .find(Song.class);

多数据库支持

动态创建数据库,酱紫:

LitePalDB litePalDB = new LitePalDB("demo2", 1);
litePalDB.addClassName(Singer.class.getName());
litePalDB.addClassName(Album.class.getName());
litePalDB.addClassName(Song.class.getName());
LitePal.use(litePalDB);

或者创建一个与相同配置的数据库,酱紫:

LitePalDB litePalDB = LitePalDB.fromDefault("newdb");
LitePal.use(litePalDB);

随时切换回默认数据库:

LitePal.useDefault();

删除数据库:

LitePal.deleteDatabase("newdb");

更多详情郭大神博客 http://blog.csdn.net/sinyu890807/article/category/2522725
LitePal地址 https://github.com/LitePalFramework/LitePal
两篇郭大神的公众号推送
我为什么能将效率提升了800%
你们要的多数据库功能终于来了

litepaldemo's People

Contributors

wuxiaosu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

litepaldemo's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.