Novice diary, I hope Xiaobai, like me, can get some harvest after reading it, and I hope someone can help me find problems! Can help me more in-depth principle, I am pure Xiaobai!
catalogue
1.2 what's the use of LitePal?
2, Configure LitePal to the current project
2.2 how to configure the configuration steps?
1, About LitePal
1.1 what is LitePal?
LitePal is a library. What is the library? Recall that the library in IDEA seems to be a library, and then we will add many jar packages to it. For example, we will add mysql jars, and then we can directly use various classes in the library. LitePal is an open source library. We got it on github.
Then it adopts the pattern of object relational mapping (orm), which is an object-oriented idea.
1.2 what's the use of LitePal?
Its use is that we can crud the data with the idea of object-oriented, instead of writing sql statements with frequent errors, we can directly operate the database under the current app, create and upgrade database tables, and crud the data. It greatly improves the efficiency of development.
In fact, here is to define java classes (entity classes, JavaBeans) as a table in the database, and the attributes in this class are the fields in the table. The operation on attribute values is to operate on the data in the table!
2, Configure LitePal to the current project
2.1 why?
Because this is not built in Android, many of which are not built in Android or need to be introduced to use. Like Android x, these are... All need to be imported.
2.2 how to configure the configuration steps?
First find the address of the GIT 2.2 library, and then find the address of the GIT 2.2 library.
'org.litepal.guolindev:core:3.2.3' [[question] here org What do you mean, a little familiar with... Seems to be with com Is it the same thing, domain name?
2.2.2. Build. In app directory Gradle is configured in a closure, also known as adding dependencies
implementation 'org.litepal.guolindev:core:3.2.3'. org.litepal.guolindev It's all the same. It should be and com.example.litepaltest Almost, in the back core Is the version number.
2.2.3. Then you need to create an asset assets directory under / app/src/main /. In fact, this means that this is our "asset" and we can use it.
2.2.4 then create a litepal in the asset directory xml file (note here that you need to create a normal file and name it. xml at the end. Do not use the shortcut in as to create Android Resource File.xml)
2.2.5 , then in litepal The configuration information in XML indicates that this is the database and some tables we want to operate with litepal!
dbname represents a database that we use this litepal to operate, version represents the version, and the following list is the table we want to operate. It just exists in the form of a javabean object, which is also the feature of this litepal. Mapping means mapping! A class maps to a table, and the properties of the class map to the fields of the table!
<?xml version="1.0" encoding="utf-8"?> <litepal> <dbname value="BookStore" ></dbname> <version value="1" ></version> <list> <mapping class="com.example.litepaltest.Book"></mapping> </list> </litepal>
2.2.6 finally, we need to add a name attribute in the application manifest file, so that our current application can use the LitePal.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.litepaltest"> <application android:name="org.litepal.LitePalApplication" ............ </application> </manifest>
After completing the above operations, we can even use this LitePal to initialize (create and upgrade) our libraries and tables.
3, Create database
Compared with the previous complex steps, creating a database using LitePal requires only one line of code. But before creating, we must ensure that the database has not been created before it can be created. And it needs to be in LitePal. In the assets directory in advance The name of the database < dbname > and the name of the table < mapping > and the version number < version > are declared in the XML file. Then call connector. From anywhere The getdatabase () method will return a database object. If the database is not created, it will be created, but if it is created, it will not be created. The bottom layer should be to call the constructor creation library and onCreate method initialization library, which is equivalent to encapsulation.
The following is a javabean class, corresponding to a table.
public class Book { private int id; private String author; private double price; private int pages; private String name; //set and get methods
Call connector The getdatabase () method will return a writable database object. If the database does not exist, it will be created. Connector: The connector to connect database provided by LitePal. Users can use this class to get the instance of SQLiteDatabase. A class provided by litepal to connect to the database, through which you can get an sqlitedatabase instance.
create_database.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Connector.getDatabase(); });
3, Upgrade database
Upgrading data is mainly reflected in two aspects: 1. Add a new table to the library and 2. Add new fields to the original table.
Adding a new class is actually adding a new table
public class Category { private int id; private String CategoryName; private String CategoryCode; //set and get methods
Adding attributes to the original class is actually adding fields to the original table
public class Book extends LitePalSupport { private int id; private String author; private double price; private int pages; private String name; private String press;//New field
Then, if you want to upgrade, in fact, the underlying layer is to let the onUpdate method execute, so you have to let the system detect that the version has changed. At this time, we will go to litepal The version has been changed in the XML file. Moreover, adding one more table adds one more mapping relationship, which also needs to be added.
<?xml version="1.0" encoding="utf-8"?> <litepal> <dbname value="BookStore" ></dbname> <version value="2" ></version> <list> <mapping class="com.example.litepaltest.Book"></mapping> <mapping class="com.example.litepaltest.Category"></mapping> </list> </litepal>
Then execute connector again When using the getdatabase () method, the system finds that the version number has changed, and then executes the onUpdate () method.
update_database.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Connector.getDatabase(); } });
Then, the above operations will create and upgrade the database using connector After the getdatabase () method is executed, the next step is to crud the database.
4, Add data
1. First, let our java class, that is, the java class corresponding to the table in the library, inherit litepalsupport. LitePalSupport: LitePalSupport connects classes to SQLite database tables ; This class is used to connect tables in the database. In the context of an application, these classes are commonly referred to as models. ; These classes are also called models; LitePalSupport relies heavily on naming in that it uses class and association names to establish mappings between respective database tables; LiteSupport relies heavily on naming, that is, the class name is the table name. (note that this is different from that in the book, and DataSupport has been abandoned.)
import org.litepal.crud.LitePalSupport; public class Book extends LitePalSupport{ //code snippet }
2. Then we need to create the object of this class, because it inherits from LiteSupport, which means it is also a LiteSupport. Then this object can operate on a table with the same name as the class. Then call the set method of the object to add data to the field of the table, and finally remember to use the save () method to save it. Very easy to understand.
add_data.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Book book = new Book(); book.setId(1); book.setName("how to win"); book.setPages(600); book.setPrice(12.5); book.setPress(null); book.save(); } });
In this way, the data is added. Using the adb command, you can find that the table in the database has data.
5, Update data
There are several methods. One important thing is that you must know where to specify which table in which library. Here, the library is in litepal XML indicates that the table is indicated by the class name.
1) the first is to find the original storage object to update the data in it, and save again will overwrite it.
2) the second is to call the updateAll () method of this object to update all entries that meet the conditions we write.
3. The third method is the latest, which is to directly operate with LitePal (like deletion, they are the latest. There are two methods, one is to delete one item according to id, and the other is to delete all qualified items according to conditions).
update_data.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // The first way to update data in a table // Book book = new Book(); // book.setName("how to fail"); // book.setPages(1); // book.setPrice(199.9); // book.setAuthor("jackson li"); // book.setPress("unknown"); // book.save(); // book.setAuthor("jack ma"); // book.save(); // The second way to update data in a table Book book = new Book(); book.setAuthor("jack chen"); book.setPrice(299.9); book.updateAll("name=? and author=?","how to fail","jack ma"); } });
6, Delete data
Deleting data is also different from that in the book. It mainly uses LitePal to directly delete a single or conditional deletion. Call the delete () method and the deleteAll () method.
deleteAll() Parameter Description: the first is affirmative, which is the table name, the second is where, and the third is the value of the question mark in where.
The delete () method is a table name and an id.
delete_data.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { LitePal.deleteAll(Book.class,"price<?","20"); } });
7, Query data
You can query data directly by using LitePal. Call LitePal's find and findAll() methods. Find finds a javabean object, then findAll can find it through conditions, and returns a collection.
query_data.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //The first method is to find the name of an incoming table and the id of the entry to be queried // Book book = LitePal.find(Book.class,2); // Log.d(TAG, "author name" + book.getAuthor()); //The second way is to find all // List<Book> books = LitePal.findAll(Book.class); // for (Book book:books) { // Log.d(TAG, "Book Name:" + book.getName()); // } //The third way is to find all qualified List<Book> books = LitePal.where("name=?","how to fail").find(Book.class); for (Book book:books){ Log.d(TAG, "title:"+book.getName()); } } });
The above are some crud operations. They are very simple, but they still need some understanding
summary
This novice diary mainly summarizes LitePal, how to create a library, initialize the library, add, delete, check and modify the table content, many of which are inconsistent with those in the book. You need to go directly to github to learn directly!