转载请注明出处:http://blog.csdn.net/mr_leixiansheng/article/details/77163225
步骤:
1、引入LitePal依赖 ,在Gradle(app)–>dependencies 中 引入依赖:implementation 'org.litepal.android:java:3.0.0'
并在AndroidManifest.xml–>application 中添加 :android:name="org.litepal.LitePalApplication"
2、AS app 下创建 assets 文件夹,新建 litepal.xml 文件。并配置好dbname、version、list
3、新建类:作为表格。设置好set、get。在1中list引入此类
4、在主活动中进行库创建和数据的增删改查
代码如下:
1、引入依赖、环境搭建
dependencies {implementation 'org.litepal.android:java:3.0.0'
}
manifest下添加 或在 Application中初始化
<applicationandroid:name="org.litepal.LitePalApplication"></application>
LitePal.initialize(this);
2、package模式下,在app–>main下 创建 assets 文件夹,新建 litepal.xml 文件(litepal.xml需要手动移动到assets文件夹下)
<?xml version="1.0" encoding="utf-8"?>
<litepal><dbname value="bookstore" ></dbname><version value="1"></version><list><mapping class="com.leixiansheng.litepaltest.Book"></mapping><mapping class="com.leixiansheng.litepaltest.Category"></mapping></list>
</litepal>
3、创建表格类
package com.leixiansheng.litepaltest;import org.litepal.crud.DataSupport;/*** Created by Leixiansheng on 2017/8/14.*/public class Book extends LitePalSupport {private int id;private String author;private double price;private int pages;private String name;private String press;public String getPress() {return press;}public void setPress(String press) {this.press = press;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getPages() {return pages;}public void setPages(int pages) {this.pages = pages;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}
}
package com.leixiansheng.litepaltest;import org.litepal.crud.DataSupport;/*** Created by Leixiansheng on 2017/8/14.*/public class Category extends LitePalSupport {private int id;private int categoryCode;public int getCategoryCode() {return categoryCode;}public void setCategoryCode(int categoryCode) {this.categoryCode = categoryCode;}public int getId() {return id;}public void setId(int id) {this.id = id;}
}
3、主活动使用LitePal进行数据库操作
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.leixiansheng.litepaltest.MainActivity"android:orientation="vertical"><Buttonandroid:id="@+id/create_db"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Create db"android:textAllCaps="false"/><Buttonandroid:id="@+id/add_db"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Add db"android:textAllCaps="false"/><Buttonandroid:id="@+id/update_db"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Update db"android:textAllCaps="false"/><Buttonandroid:id="@+id/update2_db"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Update2 db"android:textAllCaps="false"/><Buttonandroid:id="@+id/delete_db"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Delete db"android:textAllCaps="false"/><Buttonandroid:id="@+id/query_db"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Query db"android:textAllCaps="false"/></LinearLayout>
package com.leixiansheng.litepaltest;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.Toast;import org.litepal.crud.DataSupport;
import org.litepal.tablemanager.Connector;import java.util.List;/*** LitePal:更快捷地操作数据库*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener{private final static String TAG = "MainActivity";private Button btnCreateDB;private Button btnAddBookData;private Button btnUpdateBookData;private Button btnUpdateBookData2;private Button btnDeleteData;private Button btnQueryData;private Book book;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();setOnclick();}private void setOnclick() {btnCreateDB.setOnClickListener(this);btnAddBookData.setOnClickListener(this);btnUpdateBookData.setOnClickListener(this);btnUpdateBookData2.setOnClickListener(this);btnDeleteData.setOnClickListener(this);btnQueryData.setOnClickListener(this);}private void initView() {btnCreateDB = (Button) findViewById(R.id.create_db);btnAddBookData = (Button) findViewById(R.id.add_db);btnUpdateBookData = (Button) findViewById(R.id.update_db);btnUpdateBookData2 = (Button) findViewById(R.id.update2_db);btnDeleteData = (Button) findViewById(R.id.delete_db);btnQueryData = (Button) findViewById(R.id.query_db);book = new Book();}@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.create_db: //创建数据库Connector.getDatabase();
// SQLiteDatabase sqLiteDatabase = Connector.getDatabase();break;case R.id.add_db: //添加数据book = new Book();book.setName("The Da Vinci Code");book.setPages(454);book.setAuthor("Dan Brown");book.setPress("Unknow");book.setPrice(16.96);book.save();toast("add ok");break;case R.id.update_db: //更新数据方式一book.setName("New Name");book.setAuthor("New Author");book.save();toast("update ok");break;case R.id.update2_db: //更新数据方式二(推荐)book = new Book();book.setPrice(14.95);book.setPress("Anchor");//更新条件筛选//book.updateAll(); //将所有数据还原为初始值book.updateAll("id = ? and author = ?", "1", "Dan Brown"); //id为1 且作者为author 会被更改toast("update2 ok");break;case R.id.delete_db: //删除数据//LitePal.deleteDatabase(Book.class); //删除所有数据LitePal.deleteAll(Book.class, "price > ?", "15");toast("delete ok");break;case R.id.query_db: //查询所有List<Book> bookList = LitePal.findAll(Book.class);for (Book book:bookList) {Log.i(TAG, "book name:" + book.getName());Log.i(TAG, "book author:" + book.getAuthor());Log.i(TAG, "book pages:" + book.getPages());Log.i(TAG, "book price:" + book.getPrice());Log.i(TAG, "book press:" + book.getPress());}/* 其他查询方式List<Book> bookList = DataSupport.findFirst(Book.class); //查询第一条List<Book> bookList = DataSupport.findLast(Book.class); //查询最后一条List<Book> bookList = DataSupport.where("pages > ?", "400"); //条件查询List<Book> bookList = DataSupport.order("price desc"); //查询后排序 desc降序 asc升序........*/}}private void toast(String str) {Toast.makeText(this, str, Toast.LENGTH_SHORT).show();}
}