Android第三方框架LitePal的使用

1、app:添加依赖

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

2、assets中创建litepal.xml(直接复制代码使用)

<litepal>
  //数据库名字
    <dbname value="BookStore" ></dbname>

    <version value="1" ></version>
    <list>
    //对应实体类的表名(需要的表都在下面添加)
        <mapping class="com.example.administrator.litepal.Book"></mapping>
    </list>
</litepal>

3、需要使用的实体类必须继承 DataSupport

public class Book extends DataSupport {
    private int id;
    private String author;
    private double price;
    private int pages;
    private String name;
    //添加get set方法
    }

4、添加Application
①直接manifest添加name:org.litepal.LitePalApplication
②有自定义的Application集成org.litepal.LitePalApplication然后添加manifest

5、增删改插直接看代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    /**
     * 创建数据库
     */
    private Button mCreate;
    /**
     * 添加表数据
     */
    private Button mSave;
    /**
     * 更新表数据
     */
    private Button mUpdate;
    /**
     * 删除表数据
     */
    private Button mDelete;
    /**
     * 查找表数据
     */
    private Button mQuery;
    /**
     * TextView
     */
    private TextView mTextView;

    private  int count=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        mCreate = (Button) findViewById(R.id.create);
        mCreate.setOnClickListener(this);
        mSave = (Button) findViewById(R.id.save);
        mSave.setOnClickListener(this);
        mUpdate = (Button) findViewById(R.id.update);
        mUpdate.setOnClickListener(this);
        mDelete = (Button) findViewById(R.id.delete);
        mDelete.setOnClickListener(this);
        mQuery = (Button) findViewById(R.id.query);
        mQuery.setOnClickListener(this);
        mTextView = (TextView) findViewById(R.id.textView);
    }

    @Override
    public void onClick(View v) {
        Book book;
        switch (v.getId()) {
            default:
                break;
            case R.id.create:
                //创建数据库
                Connector.getDatabase();
                break;
            case R.id.save:
                Book  books = new Book();
                books.setId(1+count);
                books.setName("Code"+count);
                books.setAuthor("Dan"+count);
                books.setPages(454+count);
                books.setPrice(16.96+count);
                boolean flag=books.save();
                mTextView.setText("保存数据:"+flag);
                count++;
                break;
            case R.id.update:
                book = new Book();
                book.setPrice(14.95);
                int i= book.updateAll("name = ? and author = ?", "Code1", "Dan1");
                mTextView.setText("更新数据:"+i);
                break;
            case R.id.delete:
               int b= DataSupport.deleteAll(Book.class, "price < ?", "15");
                mTextView.setText("删除数据:"+b);
                break;
            case R.id.query:
                List<Book> books1 = DataSupport.findAll(Book.class);
                StringBuffer sb=new StringBuffer();
                sb.append("查询数据"+books1.size()+"条--");
                for (Book book1 : books1) {
                    sb.append("name:" + book1.getName()
                    + "-author:" + book1.getAuthor()
                    +"-pages:" + book1.getPages()
                    + "-book:" + book1.getPrice()+"\n");
                }
                mTextView.setText(sb.toString());
                break;
        }
    }
}

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"
    tools:context=".MainActivity"
    android:orientation="vertical">


    <Button
        android:id="@+id/create"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="创建数据库" />
    <Button
        android:id="@+id/save"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="添加表数据" />
    <Button
        android:id="@+id/update"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="更新表数据" />
    <Button
        android:id="@+id/delete"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="删除表数据" />
    <Button
        android:id="@+id/query"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="查找表数据" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

效果图:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/u012901807/article/details/86654329