"The speed of light using Open source Framework series" Database Framework Ormlite

Source: Internet
Author: User

"About this series"

Recently saw a lot of open source framework, online information is also very much, but I think the best way to understand a framework is actually used. This series of blog is to lead you to quickly get started with some common open source framework, experience its role.

Due to the limited author level, this series will only describe how to quickly use the basic functions of the framework, and more detailed settings can be found on the pages of these items.

"Introduction":

Ormlite is a framework for supporting the development of databases, you can quickly set up a data table by configuring annotations to entities, or you can quickly manipulate the database using the methods provided by the DAO class, such as additions and deletions.

"Project page":

http://ormlite.com/

"Use steps"

1. Download the Android and core jar packages from http://ormlite.com/releases/and import them into the project

2. Creating an Entity class

3. Configure annotations for the entity class (indicating, field names, field properties, etc.)

4. Create a Openhelper class inheritance Ormlitesqliteopenhelper implement OnCreate and Getdao methods

5. After completing the above steps, you can use our Openhelper class to get the DAO to operate.

Code

Entity class Person.java

@DatabaseTable (tableName = "Person")//Set Generate table name person Public classPerson {@DatabaseField (Generatedid=true)//Set Build IDPrivate intId; @DatabaseField (ColumnName= "Name")//Set field namePrivateString name;  PublicPerson () {} PublicPerson (String s) { This. Name =s; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }}

Custom Openhelper class Openhelper.java

 Public classOpenhelperextendsOrmlitesqliteopenhelper {PrivateDao<person, integer>Mpersondao;  PublicOpenhelper (Context context) {Super(Context, "test",NULL, 1);//Initialize, database name is test} @Override Public voidonCreate (sqlitedatabase sqlitedatabase, Com.j256.ormlite.support.ConnectionSource connectionsource) {Try{tableutils.createtable (Connectionsource, person.class);//build a table based on the configured entity class}Catch(SQLException e) {e.printstacktrace (); }} @Override Public voidOnupgrade (sqlitedatabase sqlitedatabase, Com.j256.ormlite.support.ConnectionSource Connectionsource,intIintI1) {    }     PublicDao<person, integer> Getpersondao ()throwsJava.sql.SQLException {//This method is used to return the DAO classif(Mpersondao = =NULL) {Mpersondao= Getdao (person.class); }        returnMpersondao; }}

Test class Test.java

 Public classTestextendsAndroidtestcase { Public voidTestadd () {person P1=NewPerson ("U3"); Openhelper Helper=NewOpenhelper (GetContext ());//Create a new Openhelper objectTry{Helper.getpersondao (). Create (P1);//Add a record P1=NewPerson ("u31"); Helper.getpersondao (). Create (P1);//Add a record P1=NewPerson ("u32"); Testlist ();//querying and typing all records}Catch(SQLException e) {e.printstacktrace (); }    }     Public voidtestlist () {Openhelper helper=NewOpenhelper (GetContext ());//Build Openhelper ObjectTry{List<Person> users =Helper.getpersondao (). Queryforall ();//Use DAO to query all records LOG.V ("SK", users.tostring ());//print in logo}Catch(SQLException e) {e.printstacktrace (); }    }}

"A more complicated example."

The above is the simple use of the ormlite framework, but this usage is still very humble, if you want to continue to study, you can look at this blog

Android Rapid Development Series Ormlite Framework Best Practices

http://blog.csdn.net/lmj623565791/article/details/39122981

"The speed of light using Open source Framework series" Database Framework Ormlite

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.