1. Introduction to LitePal
LitePal is an open source Android database framework. It adopts the object-relational mapping (ORM) model and encapsulates some of the most commonly used database functions in our daily development. Yes, various constructions can be completed without writing a single line of SQL statements. Table and CRUD operations.
2. Configuration of LitePal
The first step is to edit the app/build.gradle file and add the following to the dependencies closure
compile'org.litepal.android:core:1.4.1' //1.4.1 means the version number, the latest version number can be found on the project homepage of LitePal
The second step is to 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 litepal.xml file. content, as shown in the figure
<?xml version="1.0" encoding="utf-8" ?> <litepal> <!-- database name--> <dbname value="Student"></dbname> <!-- version number--> <version value="1"></version> <!-- create table--> <list> <mapping class="full class name of the mapped javaBean"></mapping> </list> </litepal>Finally, configure the Application and modify the code in AndroidManifest.xml, as shown in the figure
<application android:name="org.litepal.LitePalApplication" //Add this line ... </application>
3. Create database and tables
1). A JavaBean object is required, which is a database table. For example, I want to create a Student table, as shown in the figure
public class Student extends DataSupport{ private int id; private String name;//姓名 private int studentId;//学号 private String sex;//gender 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 getStudentId() { return studentId; } public void setStudentId(int studentId) { this.studentId = studentId; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }2). Modify the code in litepal.xml, as shown below
<?xml version="1.0" encoding="utf-8" ?>
<litepal>
<!-- database name-->
<dbname value="Student"></dbname>
<!-- version number-->
<version value="1"></version>
<!-- create table-->
<list>
<mapping class="com.example.litepal.Student"></mapping> //Modify this line
</list>
</litepal>
Add a line of code to mainactivity
LitePal.getDatabase();
After running the program, you can see the created database and table in adb, or you can see that the database has been created under the file of data/data/package name
4. Add data using LitePal
Add a button in MainActivity, click the button to add data, and write in the onClick method
Student student = new Student(); student.setName(name); student.setSex(sex); student.setStudentId(Integer.parseInt(studentId)); student.save(); //The save() method is inherited from the DataSupport method. In addition to the save() method, the DataSupport class also provides rich CRUD methods
5 Delete data with LitePal
Just one line of code
DataSupport.deleteAll(Student.class, "id = ?", id +"");
The first parameter is used to specify which table to delete the data in. Student.class means to delete the data in the Student table. The latter parameters are used to specify constraints, such as
DataSupport.deleteAll(Student.class, "id < ?", "15"); //Denotes to delete rows with id less than 15
6 Updating data with LitePal
Student student = new Student(); student.setName(name); student.setSex(sex); student.setStudentId(Integer.parseInt(studentId)); student.updateAll("id = ?", id+"");
7 Querying data with LitePal
Using LitePal to query data is not complicated at all compared to sqlite. Thinking about the query() method used before, the lengthy parameter list is a headache.
Using LItePal can be achieved just by writing like this
List<Student>students=DataSupport.findAll(Student.class);
For example, to click a button to display the names of all students in the table, in the button's onClick method
List<Student>students=DataSupport.findAll(Student.class); for(Student student:students){ Log.d("MainActivity",student.getName()); }
Seeing this, there should be no problem with LitePal. If you have any questions, chat privately!
Blogging for the first time! ! ! Give a like if you think it's good!