LitePal

一、什么是LitePal

LitePal是一款开源的Android数据库框架,采用了对象关系映射(ORM)的模式,将平时开发时最常用的一些数据库功能进行了封装,使得开发者不用编写一行SQL语句就可以完成各种建表、増删改查的操作。

二、设置LitePal

1、包含库

编辑build.gradle文件并添加下面的依赖关系
dependencies {
compile ‘org.litepal.android:core:1.6.1’
}

2、配置litepal.xml

右击app/src/main目录,创建一个assets文件夹,然后在这个文件夹下创建一个litepal.xml的文件,接着编辑这个文件中的内容,如下面所示:


<litepal>
    
    <dbname value="demo" />

    
    <version value="1" />

    
    <list>
    list>

    

litepal>

3、配置LitepalApplication

为了简化API,只需要在AndroidManifest中配置LitepalApplication

    "org.litepal.LitePalApplication"
        ...
    >
        ...
    

三、使用Litepal

1、创建表格

定义模型-Album和Song

public class Album extends DataSupport {
    @Column(unique = true, defaultValue = "unknown")
    private String name;

    private float price;

    private byte[] cover;

    private List songs = new ArrayList();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    public byte[] getCover() {
        return cover;
    }

    public void setCover(byte[] cover) {
        this.cover = cover;
    }

    public List getSongs() {
        return songs;
    }

    public void setSongs(List songs) {
        this.songs = songs;
    }
}
public class Song extends DataSupport {
    @Column(nullable = false)
    private String name;

    private int duration;

    @Column(ignore = true)
    private String uselessField;

    private Album album;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getDuration() {
        return duration;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    public String getUselessField() {
        return uselessField;
    }

    public void setUselessField(String uselessField) {
        this.uselessField = uselessField;
    }

    public Album getAlbum() {
        return album;
    }

    public void setAlbum(Album album) {
        this.album = album;
    }
}

然后把这两个模型放到list中

 <list>
        <mapping class="com.example.litepal.Album" />
        <mapping class="com.example.litepal.Song" />
    list>

这些表格会在你操作数据库时自动生成

举个例子:在Activity中写入以下代码

SQLiteDatabase db = LitePal.getDatabase();

注意点:

两个模型一定要继承DataSupport

2、保存数据

                String albumName = albumEt.getText().toString();
                float price = 109.9f;
                Album album = new Album();
                album.setName(albumName);
                album.setPrice(price);
                album.save();//保存至数据库

3、修改数据

                Album album1= DataSupport.find(Album.class,1);
                album1.setName("Fantasy");//设置修改后的名称
                album1.save();

4、删除数据

  //int row=DataSupport.delete(Album.class,2);
   int row=DataSupport.deleteAll(Album.class,"id>?","2");//第一个参数用于指定删除哪个表中的数据,后面的为约束条件
  Toast.makeText(this,"您删除了"+row+"行数据",Toast.LENGTH_SHORT).show();

5、查询数据

  List albumList = DataSupport.findAll(Album.class);//查询表中所有数据
                //List albumList=DataSupport.where("id,"6","a%").order("name").limit(5).find(Album.class);
                for (Album a:
                        albumList)//写个for循环
                    Log.e("MAIN ",a.getName()+"***");

6、完整代码如下:

布局文件中

id="@+id/main_album_et"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:hint="专辑名" />

    

Activity中

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText albumEt;
    private Button addAlbumBtn;
    private Button modifyAlbumBtn;
    private Button deleteAlbumBtn;
    private Button queryAlbumBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //SQLiteDatabase db = LitePal.getDatabase();
        bindId();
    }

    private void bindId() {
        albumEt = findViewById(R.id.main_album_et);
        addAlbumBtn = findViewById(R.id.main_add_album_btn);
        modifyAlbumBtn = findViewById(R.id.main_modify_album_btn);
        deleteAlbumBtn = findViewById(R.id.main_delete_album_btn);
        queryAlbumBtn=findViewById(R.id.main_query_album_btn);
        addAlbumBtn.setOnClickListener(this);
        modifyAlbumBtn.setOnClickListener(this);
        deleteAlbumBtn.setOnClickListener(this);
        queryAlbumBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.main_add_album_btn:
                String albumName = albumEt.getText().toString();
                float price = 109.9f;
                Album album = new Album();
                album.setName(albumName);
                album.setPrice(price);
                album.save();//保存至数据库
                DebugDB.getAddressLog();
                break;
            case R.id.main_modify_album_btn:
                Album album1= DataSupport.find(Album.class,1);
                album1.setName("Fantasy");
                album1.save();
                break;
            case R.id.main_delete_album_btn:
                //int row=DataSupport.delete(Album.class,2);
                int row=DataSupport.deleteAll(Album.class,"id>?","2");
                Toast.makeText(this,"您删除了"+row+"行数据",Toast.LENGTH_SHORT).show();
                break;
            case R.id.main_query_album_btn:
                List albumList = DataSupport.findAll(Album.class);
                //List albumList=DataSupport.where("id
                for (Album a:
                        albumList)
                    Log.e("MAIN ",a.getName()+"***");
                break;
        }
    }
}

你可能感兴趣的:(LitePal)