Use of Android database Liteorm

Source: Internet
Author: User

1. What is Liteorm

Liteorm is a small, powerful, 1 time times faster than the system with the database operation performance of the Android ORM Framework class Library, the developer of a line of code to implement the database additions and deletions to the operation, as well as the persistence of entity relationships and automatic mapping.

2. GitHub Address

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

3. Use

Download the latest Jar package and import it into the project

4. Create an ORM instance

A database corresponds to an instance of Liteorm, if an app has only one database, then liteorm should be a global singleton. If you create a new Liteorm instance more than once, you will be prompted to close the previous database and may cause other unknown errors.

To maintain a single case:

Liteorm Liteorm;   null) {    liteorm. Newsingleinstance ("liteorm.db");} Liteorm. setdebugged (//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.sav    E (list); /** * Query All * @param CLA * @return */public <T> list<t> Getqueryall (class<t> CLA)    ) {return liteorm.query (CLA); /** * Query the value of a field equal to value * @param cla * @param field * @paraM value * @return */public <T> list<t> Getquerybywhere (class<t> cla, String field, string[]    Value) {return liteorm.<t>query (new QueryBuilder (CLA). where (field + "=?", value)); /** * Query a value with a field equal to value can be specified from 1-20, that is, paging * @param cla * @param field * @param value * @param star T * @param length * @return * * Public <T> list<t> getquerybywherelength (class<t> CLA, Str  ing field, string[] value, int start, int length) {return liteorm.<t>query (new QueryBuilder (CLA). where (field    + "=?", value). Limit (start, length)); }/** * Delete a data * @param t * @param <T> * * Public <T> void Delete (T t) {LITEORM.D    Elete (t);         }/** * Delete a table * @param CLA * @param <T> * * Public <T> void Delete (class<t> cla) {    Liteorm.delete (CLA);    /** * Delete Data in collection * @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 (Savedinstan        Cestate);        Setcontentview (R.layout.activity_main);        Final Testmodel Testmodel = new Testmodel ();        Testmodel.setid (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 Data databasemanager.getinstance (). Insert (Testmodel); Insert aCollection Databasemanager.getinstance (). Insertall (list);        Query all list<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);        }        });            Deletes a collection Findviewbyid (r.id.delete_database). Setonclicklistener (New View.onclicklistener () {@Override            public void OnClick (view view) {databasemanager.getinstance (). DeleteList (list);        }        });            Delete a table Findviewbyid (r.id.delete_table). Setonclicklistener (New View.onclicklistener () {@Override            public void OnClick (view view) {databasemanager.getinstance (). Delete (Testmodel.class);        }        }); Delete the entire database FindviewbyId (r.id.delete_database). Setonclicklistener (New View.onclicklistener () {@Override public void OnC            Lick (view view) {databasemanager.getinstance (). DeleteDatabase ();    }        }); }}

  

5. The relevant code has been uploaded to GitHub

Reference

Android ORM Framework Liteorm use

Use of Android database 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.