Android LiteOrm, androidliteorm

Source: Internet
Author: User

Android LiteOrm, androidliteorm
1. What is LiteOrm?

LiteOrm is a small, powerful android ORM framework class library that is twice as powerful as the system's built-in database. developers can add, delete, modify, and query databases with a single line of code, and the persistence and automatic ing of Object Relationships.

 

2. github address

Https://github.com/litesuits/android-lite-orm

 

3. Use

Download the latest jar package and import it to the project.

 

4. Create An orm instance

A database corresponds to an LiteOrm instance. If an App has only one database, LiteOrm should be a global Singleton instance. If you create a LiteOrm instance multiple times, the system will prompt you to shut down the previous database or cause other unknown errors.

Persistence singleton:

static LiteOrm liteOrm;if (liteOrm == null) {    liteOrm = LiteOrm.newSingleInstance(this, "liteorm.db");}liteOrm.setDebugged(true); // open the log

 

For ease of use in the project, we created a DatabaseManager tool class.

 

Package com. app. liteorm; import com. litesuits. orm. liteOrm; import com. litesuits. orm. db. assit. queryBuilder; import java. util. list;/*** Created by $ {zyj} on 2016/7/4. */public class DatabaseManager {private static LiteOrm liteOrm; private static DatabaseManager ourInstance = new DatabaseManager (); private DatabaseManager () {liteOrm = LiteOrm. newCascadeInstance (MyAppLication. getContext (), "wifi. db" );} Public static DatabaseManager getInstance () {return ourInstance;}/*** insert a record * @ param t */public <T> long insert (T t) {return liteOrm. save (t);}/*** insert all records * @ param list */public <T> void insertAll (List <T> list) {liteOrm. save (list);}/*** query all * @ param linoleic * @ return */public <T> List <T> getQueryAll (Class <T> linoleic) {return liteOrm. query (linoleic);}/*** query the Value of a field equal to Value * @ param linoleic * @ Param field * @ param value * @ return */public <T> List <T> getQueryByWhere (Class <T> linoleic, String field, String [] value) {return liteOrm. <T> query (new QueryBuilder (CIA ). where (field + "=? ", Value);}/*** to query the Value of a field equal to value, you can specify the Value from 1 to 20, it is paging * @ param linoleic * @ param field * @ param value * @ param start * @ param length * @ return */public <T> List <T> getQueryByWhereLength (Class <T> linoleic, string field, String [] value, int start, int length) {return liteOrm. <T> query (new QueryBuilder (CIA ). where (field + "=? ", Value ). limit (start, length);}/*** delete a data * @ param t * @ param <T> */public <T> void delete (T t) {liteOrm. delete (t);}/*** delete a table * @ param linoleic * @ param <T> */public <T> void delete (Class <T> linoleic) {liteOrm. delete (CIA);}/*** delete data in the Set * @ param list * @ param <T> */public <T> void deleteList (List <T> list) {liteOrm. delete (list);}/*** delete database */public void deleteDatabase () {liteOrm. deleteDatabase ();}}
Use
Package com. app. liteorm; import android. support. v7.app. appCompatActivity; import android. OS. bundle; import android. view. view; import java. util. arrayList; import java. util. list; public class MainActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); final TestModel testModel = new TestModel (); testModel. setids (1001); testModel. setName ("jack"); testModel. setPassword ("123456"); testModel. setLogin (true); TestModel testMode2 = new TestModel (); testMode2.setId (1002); testMode2.setName ("jack2"); testMode2.setPassword ("123456"); testMode2.setLogin (false ); final List <TestModel> list = new ArrayList <> (); list. add (testModel); list. add (testMode2); // insert a piece of data DatabaseManager. getInstance (). insert (testModel); // insert a collection DatabaseManager. getInstance (). insertAll (list); // query all lists <TestModel> queryList = DatabaseManager. getInstance (). getQueryAll (TestModel. class); // delete a data findViewById (R. id. delete_object ). setOnClickListener (new View. onClickListener () {@ Override public void onClick (View view) {DatabaseManager. getInstance (). delete (testModel) ;}}); // delete a set of findViewById (R. id. delete_database ). setOnClickListener (new View. onClickListener () {@ Override public void onClick (View view) {DatabaseManager. getInstance (). deleteList (list) ;}}); // deletes a table findViewById (R. id. delete_table ). setOnClickListener (new View. onClickListener () {@ Override public void onClick (View view) {DatabaseManager. getInstance (). delete (TestModel. class) ;}}); // deletes the findViewById (R. id. delete_database ). setOnClickListener (new View. onClickListener () {@ Override public void onClick (View view) {DatabaseManager. getInstance (). deleteDatabase ();}});}}

  

5. Related code has been uploaded to github

 

Reference

Use of the Android ORM framework LiteOrm

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.