SQlite database framework: LitePal

Common database framework
pace of Android is incredible, Android out what year I was still in primary school is very, very clearly remember also, that in everything, but at that time I could not think how their future Android.Android database framework will have to do a lot, choose your favorite, do not grasp a lot, like to choose their favorite.

Sugar&GreenDao&Afinal&ORMLite&Realm&LiteOrm&DBFlow&ActiveAndroid&LitePal

SugarORM is object-relational mapping mode. Do not write complex sql statement, but a simple API to complete the creation and manipulation of data; you can add small changes in the original Bean Bean used only undone; and clear to simplify database design and creation process while providing table many support.

GreenDao for Android is designed for object-relational mapping (ORM) tool. It provides object to the appropriate interface to the SQLite relational database. In order to use greenDao in the Android project, we need to create another "builder" project, its task is to generate specific code in your project domain. It has superior properties as compared with other ORM framework.

Afinal is an android sqlite orm and the ioc framework. It encapsulates both http android in the framework to make it more simple to use. FinalDB module, android orm the frame, one line of code can be CRUD. Support many, many to one and other inquiries.

ORMLite (Object Relational Mapping Lite) provides some lightweight persistence of Java objects to SQL databases, while avoiding the complexity and more of a standard package of ORM overhead functions. It supports SQL database using JDBC quantity, but also supports the Android operating system, database native API calls sqlite.

Realm is a solution used to replace the sqlite, it will have its own database storage engines, more lightweight than sqlite, has a faster speed, and has many features of modern databases, such as support for JSON, streaming api, data change notification, automatic data synchronization, simple authentication, access control, event processing, the most important thing is cross-platform, there are Java, Objective C, Swift, React-Native, Xamarin five achieved.

LiteOrm is a database (ORM) framework library on android. Speed, small size and high performance. Developers implement the basic line of code to change the database search operation deletions, as well as persistent entities and relations are automatically mapped. Design principles: lightweight, focused, performance priority, regardless of the thread, and the relationship data storage and focusing operations; without auxiliary tools, no constructor with no arguments, does not require many annotations, convention over configuration; extreme simplicity of use, for example: DB .save (u); db.query (U.class); db.deleteAll (U.class) ;.

DBFlow combines the advantages of ActiveAndroid, Schematic, Ollie, Sprinkles and other libraries. While not based on reflection, so performance is very high, efficiency greenDAO followed suit. Based on annotation, using techniques apt to generate during compilation operations, a highly similar ActiveAndroid use and simple to use. Seamless support for multiple databases, using the annotation processing to improve speed, ModelContainer library can parse the data directly such as JSON, increase the flexibility of rich interfaces.

ActiveAndroid is to use Active Record (Active Record) architectural pattern designed lightweight ORM framework applicable to Android platform

LitePal is an object-relational mapping (ORM) model. It enables developers to use SQLite databases very easily. You can not write a SQL statement can be completed most database operations, including creating tables, update tables, constraints operation, aggregation and so on.

 
LitePal
LitePal integrated and simple to use
LitePal implemented CRUD
LitePal describes some common API
 
LitePal integration and use of a simple
introduction of the corresponding library
compile 'org.litepal.android:core:1.6.1'
The project can go find the latest source code library

Litepal.xml create the appropriate files
First of all the files in the folder under the corresponding assets, then the assets folder (method of creating the project folder New-> Folder-> Assets Folder can create the appropriate folder for the assets) litepal.xml next create a corresponding folder is the following specific content:

<?xml version="1.0" encoding="utf-8"?>
<litepal>
<dbname value="file"/>
<version value="1"/>
<list>
<mapping class="com.hejin.download.FileInfo"/>
</list>
</litepal>
这里说明几点问题:

dbname tag is appropriate database name

label version is the version of the corresponding database, where attention when the contents of which reference is changed, the upgrade to the corresponding version

Those in need of tag list is a database management, where attention to mapping the inside of the path must be a full path name

This label inside the entity class list in this category should inherit DataSupport, do not forget this

Application to replace the inherited
here is actually part two situations:

It has inherited Application

The direct successor LitePalApplication

Why is divided into these two cases? Because before implementing any project in Application may have inherited one of the other types of Application, then this time because single inheritance java, so you can not inherit LitePalApplication here directly, but has helped LitePal we thought about this problem, so let's say two cases to achieve this:

The first

OnCreat directly in the corresponding item in the Application () method to initialize click on it, like this:

@Override
public void the onCreate () {
super.onCreate ();
LitePal.initialize (the this);
}
so that you can easily inherit the Application Other Application

The introduction of the appropriate Application in manifest.xml, that is your previous Application

The second

The direct successor to the corresponding LitePalApplication here without calling LitePal.initialize (this) in onCreat (); this is the method;

This is more special to be added in the corresponding application Application tab when you want to write android: name = "org.litepal.LitePalApplication"

 
LitePal achieve CRUD (CRUD)
LitePal add data
about a corresponding increase in operation here is mainly carried out by save object (), how do you say? In fact, you just give the corresponding class adds the appropriate parameters and then call the appropriate collective the save () method can be a specific code is as follows:

LitePalBean zhangSanBean = new LitePalBean ( "San", "20", "M");
zhangSanBean.save ();
LitePalBean liSiBean new new LitePalBean = ( "John Doe", "25", "M");
liSiBean.save ( );
. this creates a corresponding database table structure, that is, with the corresponding database, but if you are debugging on the phone Root words are not invisible here instructions to add the same content can be a problem added to it

LitePal data delete
operation to delete the data, this is the main object deleted by delete () or deleteAll (), the specific code is as follows:

/ * This is to delete all names are Joe Smith * /
DataSupport.deleteAll (LitePalBean.class, "Joe Smith" "name =?");

LitePalBean zhangSanBean = new new LitePalBean ( "Joe Smith", "20", "men ");
zhangSanBean.save ();
zhangSanBean.delete ();
here deleted in two ways:

Delete the direct object: the object is a direct call delete (); method;

Delete according to the type, in fact, this is the equivalent of deleting SQLite, here it is necessary to explain the second statement incoming content if "name =?" Represents the name of all the content behind the field, that is, all named Zhang three content will be deleted. then you will ask me if there is an incoming two content? such as "name =? and age =?" to Yes, that is connected with and

LitePal update data
here is very simple, it is appropriate to change the complete data stored directly save it, the code is as follows:

LitePalBean zhaoWuBean = new LitePalBean ( "Zhao Five", "25", "male");
zhaoWuBean.save ();
zhaoWuBean.setAge ( "30");
zhaoWuBean.save ();
There is a problem, I feel should be the first to take this object fishes out during the operation in order to ensure the correctness of the contents.

LitePal query methods
operating on query data, mainly used find () and the findAll () method and the like corresponding to the specific code is as follows:

List <LitePalBean> All = DataSupport.findAll (LitePalBean.class);
for (int I = 0; I <all.size (); I ++) {
Log.e (the TAG, "Query:" + all.get (I) .toString ());
}
}
 
LitePal common the API
DataSupport.findFirst (XXX.class)
Discover the first element in the list XXX

DataSupport.findLast (XXX.class)
query the last element in the list XXX

DataSupport.select ()
to find the keywords based on SQLite, for example DataSupport.select ( "name", "age "). Find (XXX.class) query is two data corresponding name and age of the other data is checked not out.

DataSupport.where ()
is equivalent to where keyword search in SQLite, here is an example - ( "? Name =", " Joe Smith")> DataSupport.where .find (XXX.class) query is a name for all John's data content;

DataSupport.order ()
corresponds to the order in SQLite keyword lookup, sort primarily, here is an example -> DataSupport.order (price desc) .find (XXX.class) there is arranged in descending order; desc identification descending order, ASC **
** Representative ascending

DataSupport.limit ()
number of examples of the results of the database query -> DataSupport.limit (3) .find ( XXX.class); query data 3

DataSupport.offset ()
query formulation example offset results (I am asking for articles II and Article IV) -> DataSupport.limit (3) .offset (1) .find (XXX.class); here We are starting from the first, but does not contain a respective first
--------------------- 

Guess you like

Origin www.cnblogs.com/ly570/p/10977614.html