Android: Daily learning notes ——— using Litepal to manipulate databasesintroduction of LitepalWhat is Litepal
Litepal is an open-source Android database framework , using the object-relational mapping (ORM) model , the most commonly used in the development of some of the database functions are encapsulated, so that developers do not have to write a line of SQL statements to complete the various tables , and the operation of the inspection and deletion. And the Litepal is "light", the jar package size is less than 100k, and almost 0 configuration , which is very different from the type of hibernate such as the framework. At present, the source code of Litepal has been hosted on GitHub.
About Object Relational mappings:
The object language we use is object-oriented language, and the database used is a relational database, which establishes a mapping relationship between object-oriented language and relational database , which is object relation mapping. Instead of writing any SQL statements, we can manipulate the database with object-oriented design ideas .
Configure Litepal
1. Add dependencies
2. Writing an XML configuration file
<?XML version= "1.0" encoding= "Utf-8"?><Litepal> <dbnamevalue= "Bookstore"></dbname>//Database name <versionvalue= "1"></version>//Database version <List></List></Litepal>
Description
<dbname>是数据库的名字<version>是数据库的版本号<list>是数据库的映射模型(数据库表)<mapping>是数据库的映射模型的地址(数据库表结构)
3. Configure Litepalapplication
< Application android:name= "Org.litepad.LitePalApplication" ..... > .... </ Application >
Creating and Upgrading Databases
Create a database and Book table
1. Define JavaBean
Public classBook {Private intID; PrivateString author; Private DoublePrice ; Private intpages; PrivateString name; Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } ..... }
2. Add the book class to the Map model list
<?XML version= "1.0" encoding= "Utf-8"?><Litepal><dbnamevalue= "Bookstore"></dbname><versionvalue= "1"></version><List> <mapping class= "Com.example.zy.dealhelper.database.Book">< /mapping> </List></Litepal>
3. Invoking a command to create a database
Public void OnClick (View v) { connector.getdatabase (); }
4.litePal automatically creates data tables for database and mapping object classes
Upgrade Database
Litepal upgrading the database is very simple, you do not need to think about any logic, just need to modify anything you want to change, and then increment the version. For example, we want to add a column to the book table, such as press (publishing house), directly in the JavaBean.
Public classBook {Private intID; PrivateString author; Private DoublePrice ; Private intpages; PrivateString name; Private String Press; // Publishing House Public String getpress () { return Press ; public void setpress (String Press) { this. Press = Press ; } ...... }
Or if you want to create a more table, you just need to write another JavaBean class.
Public class catagory { privateint ID; Private String CategoryName; Public int getId () { return ID; } Public void setId (int ID) { this. ID= id; } ...... }
Don't forget to add mapping and update version numbers in the last Litepal configuration file
Add or delete to change the data
Using Litepal to add data is simply an exception:
We just need to create the model class and set the data well, and call the Save () method.
Premise:
The data table to perform the curd operation must inherit from Datasupport
Example:
Public void OnClick (View v) { =new book (); Book.setname ("The Da Vinci Code"); Book.setauthor ("Dan Brown"); Book.setpages (454); Book.setprice (16.96); Book.setpress ("Unknow"); Book.save ();//Just call the Inheritance method, save () to }
Update data
Reset values for stored objects:
Condition Matching:
Remember to update all records if you don't specify a condition !
Delete data
Datasupport.deleteall (book. Class,"Price <?","all")
Querying data
Simple query:
// Find all data for a table List<book> Books =datasupport.findall (book. Class); // The first piece of data Book First =datasupport.findfirst. Class); // The last piece of data Book, last =datasupport.findlast. Class);
concatenating query:
Solutions for ADB startup errors:
1. cmd Command Window input: ADB nodaemon server. You will then be prompted which port is occupied.
2. Input Netstat-ano | Findstr "5037". You will then be prompted to tell you which processes are consuming the port, remembering the number after the non-0 address
3. Open Task Manager, click "Process", "View"-"Select column", check the PID
4, find the 2nd step to see the digital PID, and then end the relevant process, you can
Android: Daily Learning notes ——— using Litepal to manipulate databases