Concept: LitePal is an open source Android database framework that adopts the pattern of Object Relational Mapping (ORM). And encapsulate some of the most commonly used database functions we usually develop. LitePal detailed documentation: https://github.com/LitePalFramework.
1. Configure LitePal
Edit the app/build.gradle file
Add the following to the dependencies closure
dependencies{
……
compile 'org.litepal.android:core:1.4.1'
}
Configure the litepal.xml file.
Right-click the app/src/main directory→New→Directory, create an assets directory, and then create a new litepal.xml file in the assets directory, and then edit the content of the litepal file.
As follows:
<?xml version="1.0" encoding="utf-8?> <litepal> <dbname value="BookStore"></dbname> <version value="1"></version> <list> </list> </litepal>
The <dbname> tag is used to specify the database name, the <version> tag is used to specify the database version number, and the <list> tag is used to specify all the mapping models.
Configure the code for AndroidManifest.xml as shown below.
Will
android:name="org.litepal.LitepalApplication"
2. Create and upgrade the database
1) Create table
For example, we want to create a table about Book
First create the corresponding class
public class Book extends DataSupport{ //In order to use various operations of litepal private int id;
public int getId(){ return id; } public String getAuthor(){ return author; } public void setId(int id){ this.id=id; } public void setAuthor(String author){ this.author=author; } }
The above code is a typical Java Bean.
Next, the table needs to be
Add to list of mapping models
Add in the list tag of litepal.xml
<mapping class="com.example.litepaltest.Book"></mapping>
database creation
Just perform any database operation BookStore.db
The database should be created automatically.
Note: If you want to update the database, just add 1 to the version version under litepal.xml to complete the update.
3. Various database operations under LitePal.
add data action
Or use the previous Book as an example
private String author;
Book book=new Book(); book.setId=1; book.setAuthor("god"); book.save();
update data
book.setAuthor("human"); book.updateAll("id=?",1);
set initial value
book.setToDefault("author");//"" fill in the fields to set the initial value
delete data
1. Call the delete() of the object;
2、
DataSupport.delteAll(Book.class,"id<?","10");
Query data
List<Book> books=DataSupport.findAll(Book.class);
This statement extracts all Book objects and stores them in a linear table.
Data can be obtained by accessing the linear table.
Query the first data and the last data
Book firstBook=DataSupport.findFirst(Book.class); Book lastBook=DataSupport.findLast(Book.class);
select() is used to specify which columns of data to query
List<Book> books=DataSupport.select("author").find(Book.class);
The where() method is used to specify the constraints of the query.
The order() method is used to specify the sorting method of the results desc descending asc or not to indicate ascending order.
limit() is used to specify the number of query results. Top x bars.
The offset() method is used to specify that the offset of the query result is +n based on limit(); for example, if you want to check the first x items and use offset(1), it becomes 1+1(2) to (x+1) ) data.
Examples of comprehensive use of five sentences;
List<Book> books =DataSupport.select("id").where("id>?","20").order("id desc").limit(1).offset(5).find(Book.class);