安卓litepal

一.什么是litepal
litepal是一种第三方的插件,用于方便开发者使用内置数据库的一种工具。
二.为什么要有litepal
因为litepal是一种数据库工具,利用它可以很轻松的解决数据库问题,减少代码量,节约时间。三.如何使用litepal
首先在添加依赖环境

 compile 'org.litepal.android:core:1.6.1'

然后在project工程下找到app/src下创建一个assets文件夹,并创一个litepal.xml的file文件。如下图:
这里写图片描述
然后在litepal.xml写几行代码

<?xml version="1.0" encoding="utf-8"?>
<litepal>
            <dbname value="demo" />
    <dbname value="demo" />

            <version value="1" />
    <version value="1" />

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

    </litepal>

接着在Main函数中的xml写下控件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.ll.myx.MainActivity">

    <EditText
        android:id="@+id/ll_et_album"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
   <Button
       android:id="@+id/add_blum_cc"
       android:text="添加专辑"
       android:layout_width="match_parent"
       android:layout_height="50dp" />
    <Button
        android:id="@+id/add_blum_cc1"
        android:text="修改数据"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
    <Button
        android:id="@+id/add_blum_cc2"
        android:text="删除数据"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
    <Button
        android:text="查询专辑"
        android:id="@+id/chaxun_btn"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
</LinearLayout>

最后在住Main函数写下代码

package com.example.ll.myx;

import android.annotation.SuppressLint;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.litepal.LitePal;
import org.litepal.crud.DataSupport;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText albunet;
    private Button addbtn;
    private Button delbtn;
    private Button xiugaibtn;
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        SQLiteDatabase db = LitePal.getDatabase();

        BindId();
    }

    @SuppressLint("WrongViewCast")
    private void BindId() {
        albunet = findViewById(R.id.ll_et_album);
        addbtn = findViewById(R.id.add_blum_cc);
        xiugaibtn = findViewById(R.id.add_blum_cc1);
        delbtn = findViewById(R.id.add_blum_cc2);
        btn = findViewById(R.id.chaxun_btn);
        addbtn.setOnClickListener(this);
        xiugaibtn.setOnClickListener(this);
        delbtn.setOnClickListener(this);
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.add_blum_cc:
                String cmc = albunet.getText().toString();
                float price = 109.9f;

                Album album = new Album();
                album.setName(cmc);
                album.setPrice(price);
                album.save();//保存至数据库

                break;

            case R.id.add_blum_cc1:

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

                break;
            case R.id.add_blum_cc2:

//                int row=DataSupport.delete(Album.class,1);//只是删除一行数据
                int row = DataSupport.deleteAll(Album.class, "id>?", "3");//删除3行数据
                Toast.makeText(this, "您删除了" + row + "行数据", Toast.LENGTH_SHORT).show();

                break;
            case R.id.chaxun_btn:
//                List<Album> albumList=DataSupport.findAll(Album.class);
                List<Album> albumList = DataSupport.where("id<?", "4").find(Album.class);

                for (Album a :
                        albumList) {
                    Log.e("main", a.getName() + "********");

                }
                break;
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值