Preface : Before using Android Built in database , It feels like a lot SQL sentence , If you're not careful, you're wrong , Very afflictive , I learned this LItePal Open source database framework , For a moment, I felt Android The built-in database is rubbish
LitePal GitHub home page :https://github.com/LitePalFramework/LitePal
Configuration steps
1. Add dependency
Switch project Pattern , an APP Folder , find build.gradle file , Add dependencies to it , I'm using the latest version here , Can be in LitePal Of Github The latest version can be found on the official website
2. To configure LitePal.xml Files and related settings
stay main Create a new folder asstes Folder , after , establish LitePal.xml, Copy LitePal Upper GItHub The code given , Here I'll just write , among ,dbname Represents the name of the database ,version For version number ,list There's a table under the label ( The technical term is mapping model class ), I'll talk about it later
Besides , We still need to AndroidMainfest Add a line of code to the file android:name="org.litepal.LitePalApplication"
3. Create a Java Bean class
Here's a book created bean class , There are three member variables , after , Use alt+insert, choice getter and setter, add to get And set Method
4. stay LItePal.xml In file list Tab ( Mapping model classes )
Remember to write the package name completely
Use steps :
1. Create and upgrade databases
In relation to activity Of java Direct use of documents LitePal Of getDatabase Method to create a database , Just add the following code , Written in onClick In the event , No screenshots here
LitePal.getDatabase();
Upgrade database , Directly in Bean Of java Add a new member variable and its get And set The method can , Upgrade the database code or use the above line of code
for instance , For example, the above , We want to add a publishing column , All we need to do is Book It creates a member variable , And get And set Method , after , Go to LitePal.xml In file , take version Version plus 1, That is to say, change it to 2 that will do , Nothing else needs to be changed
for example , We want to add another table , It's a similar operation , Create a Bean class , after , stay LitePal.xml Use... In the document mapping label , meanwhile , Add one to the version number , That is to say, change it to 2 that will do , Other things don't need to change
2. Add data
Modify model class ( That is to say Book), To inherit DataSupport
after , We just need to create a new object , Call its set Method , Assign values to its member variables , after , call save Method , Is to successfully add data to the database
3. modify ( to update ) data
The operation of updating is very simple , Use udateAll The method can , for example , We're going to call it The Last The author of John, Change the price to 16
updateAll There are constraints in it , It's called The Last That piece of data , modify , If updateAll There are no parameters in it , That is to change the author and price of the data in the table to John and 16
4. Delete data
call DataSupport Of deleteAll Method , With the above updateAll similar , Parameters are filled with constraints , If you don't write , Delete the whole table
DataSupport.deleteAll(Book.class,"name is ?","The Last");// Delete name as The Last The data of
DataSupport.deleteAll(Book.class,"name is ? and price is ?","The Last","15");// Delete name as The Last, The price for 15 The data of
DataSupport.deleteAll(Book.class,"price < ?","16");// Delete price below 16 The data of
5. Find data
Look for the DataSupport Of findAll The method can , Here is the sample code
select Specify the query column ,where Find the data that meets the constraints ,order Sort the query results ,limit Limit the number of query data
List<Book> books = DataSupport.findAll(Book.class);// lookup Book All the data in this table , Back to a List
List<Book> books1 = DataSupport.select("name","price").find(Book.class);// Specify query Book In this list name And price Two columns of data , It's also a List
List<Book> books2 = DataSupport.where("price > ?","16").find(Book.class);// Specified in the Book This table looks up prices that are greater than 16( constraint condition ) The data of , Back to a List
List<Book> books4 = DataSupport.order("price desc").find(Book.class);// Sort the query results by price from high to low (desc Descending ), Don't write or asc It's in ascending order
List<Book> books5 = DataSupport.limit(3).find(Book.class);// Inquire about Book The first three data in the table
It is worth mentioning that , have access to findBySQL Method for Native Query , That's the way we used the built-in database before , Back to a Cursor object , I don't use it here
Android Development —— Use LitePal More articles on open source database framework
- Android Development —— Use LitePal Open source database
Preface : Before using Android Built in database , It feels like a lot SQL sentence , If you're not careful, you're wrong , Very afflictive , I learned this LItePal Open source database , For a moment, I felt Android The built-in database is rubbish LitePal Gi ...
- android The use of development SQLite Database storage
http://blog.csdn.net/jason0539/article/details/16360835 SQLite Introduce SQLite A very popular embedded database , It supports SQL Language , also ...
- stay Android Used in development SQLite Database notes
SQLite Introduce SQLite A very popular embedded database , It supports SQL Language , And it has good performance with very little memory . Besides, it's open source , Anyone can use it . Many open source projects ((Mozilla, PH ...
- Android Summary of third party frameworks used in development
Recently, I searched the Internet for some framework information , The following common frameworks are sorted out , Hope to help in the project . 1. Network request framework android-async-http The introduction of the network framework article address :http://www.cnblogs.com/ ...
- Android Summary of common third party frameworks used in development
This article is reprinted. :http://blog.csdn.net/liuhaomatou/article/details/44857005 1.volley Project address https://github.com/sman ...
- Android Common third party frameworks for development
1.SlidingMenu Official website :https://github.com/jfeinstein10/SlidingMenu Users use :http://blog.csdn.net/yangyu2012122 ...
- Android Development Notes : Packaging the database
For the control with more data, we usually add SQLite Database for data storage , These databases are not automatically packaged into apk Medium , How to create a database Method 1: Will create the sql Statements in SQLiteHelper Inheritance class , In the ...
- Android Development -- Database operation of data storage
brief introduction : SQLite Introduction to : SQLite Database belongs to text database , It's saved as text .Android Provide for the right to SQLite Full database support , Any class in an application can access any data by name ...
- android Open source databases are used in the project litepal
Download address https://github.com/LitePalFramework/LitePal Reference documents http://blog.csdn.net/guolin_blog/article/detai ...
Random recommendation
- 《 Serial | The Internet of things framework ServerSuperIO course 》2. Description of configuration parameters of service instance
1.C# Cross platform Internet of things communication framework ServerSuperIO(SSIO) Introduce < Serial | The Internet of things framework ServerSuperIO course >1.4 A communication mode mechanism One . review SuperIO(SIO) location ...
- HDU 3966 & POJ 3237 & HYSBZ 2243 The tree chain splits
Tree chain dissection is a very fixed routine It is generally used to solve the path change and query between two points on the tree The idea is to divide a tree into chains that you don't want to cross And because of dfs The order of The numbers given to the points or edges on each chain must be connected Then the path between each two points can be divided into several ...
- 2015 The first 43 Wednesday memcached
Memcached Is a high performance distributed memory object caching system , For dynamic Web Application to reduce database load . It reduces the number of database reads by caching data and objects in memory , To improve dynamics . Speed of database driven websites .Memcached ...
- DisplayContent、StackBox、TaskStack note
The article only records one's own understanding , For your reference only . Every display device , There is one. Display object ,DisplayManagerService It's about managing these Display. 1.DisplayContent() ...
- mongodb Compress ——3.0+ Support zlib and snappy
from :https://scalegrid.io/blog/enabling-data-compression-in-mongodb-3-0/ MongoDB 3.0 with the wired ti ...
- Java The circular basis of language ; The difference between the statements
FOR: WHILE DO WHILE break And continue The difference between break The direct interrupt statement jumps out of the loop ,continue Jump out of current loop , We will continue to carry out later
- oracle Change the date format
See the default date format select * from v$nls_parameters; change alter session | system ( Range ) set xxxx=“yyyy-mm-dd” ;
- angularjs Prevent the event from bubbling , And the attention points of instructions
appModule.directive('newStr',function(){ return{ restrict:'AE', // To prevent the event from bubbling, we need to add $event Parameters template:`<di ...
- C character string
C character string C The string in is empty ('\0') One at the end char Array , The basic ways to implement strings are : String constant , Array of strings ,char Array ,char The pointer . Strings are widely used , Such as interaction with users and other natural language processing .C by ...
- j.u.c series (10)--- The concurrency tool class :Semaphore
Write it at the front Semaphore It's a count semaphore , Its essence is a " Shared lock ". Semaphores maintain a semaphore permission set . Threads can be called acquire() To get permission for semaphores : When permission is available in the semaphore ...