Use ormlite in Android for persistence-helloormlite

Source: Internet
Author: User

Android has built-in SQLite, but the common development language Java is object-oriented, while the database is relational, every conversion between the two is very troublesome (I am not familiar with the SQL language ). Java Web development has a lot of ORM frameworks, but it is troublesome to directly use them on Android. I tried to find the android ORM framework. To tell the truth, there are several more.


Implementation Considerations: androrm Official Website: http://androrm.the-pixelpla.net/ To be honest, I really don't understand this. There are two packages in total. One is the dependency package: Apache commons-lang (2.6) Another is the main package: androrm. Jar cannot be used no matter how it is downloaded...
Then we considered db4o. Official Website: http://www.db4o.com/ According to the official website, Android is supported, but I think the package is a little large, but the speed is a little slow.
The last thing we see is ormlite. Official Website: Follow the Convention below to create an android project in Hello World: helloormlite

Add Folder: libs, copy the two required packages to it. Add reference

Create a model: Hello. Java
 
PackageCN. sdx. model;

ImportCom. j256.ormlite. Field. databasefield;

Public ClassHello {
@ Databasefield (generatedid =True)
IntID;
@ Databasefield
String word;

PublicHello (){
}

@ Override
PublicString tostring (){
Stringbuilder sb =NewStringbuilder ();
SB. append ("ID ="). append (ID );
SB. append (", word ="). append (Word );
ReturnSB. tostring ();
}

}
@ Databasefield declares that ID is a database field, generatedid = true declares that ID is auto-incrementing, and then overwrites tostring () to add a datahelper. Java
 
Package CN. sdx. utils;

Import Java. SQL. sqlexception;

Import Android. content. context;
Import Android. database. SQLite. sqlitedatabase;
Import Android. util. log;


Import CN. sdx. model. Hello;

Import Com. j256.ormlite. Android. apptools. ormlitesqliteopenhelper;
Import Com. j256.ormlite. Dao. Dao;
Import Com. j256.ormlite. Support. connectionsource;
Import Com. j256.ormlite. Table. tableutils;

Public Class Datahelper Extends Ormlitesqliteopenhelper {

Private Static Final String database_name = "helloormlite. DB ";
Private Static Final Int Database_version = 1;
Private Dao Null ;

Public Datahelper (context ){
Super (Context, database_name, Null , Database_version );
}

@ Override
Public Void Oncreate (sqlitedatabase dB, connectionsource ){
Try {
Tableutils. createtable (connectionsource, hello. Class );
} Catch (Sqlexception e ){
Log. E (datahelper. Class . Getname (), "failed to create database", e );
E. printstacktrace ();
}
}

@ Override
Public Void Onupgrade (sqlitedatabase dB, connectionsource, Int Arg2,
Int Arg3 ){
Try {
Tableutils. droptable (connectionsource, hello. Class , True );
Oncreate (dB, connectionsource );
} Catch (Sqlexception e ){
Log. E (datahelper. Class . Getname (), "failed to update database", e );
E. printstacktrace ();
}
}

@ Override
Public Void Close (){
Super . Close ();
Hellodao = Null ;
}

Public Dao Throws Sqlexception {
If (Hellodao = Null ){
Hellodao = getdao (hello. Class );
}
Return Hellodao;
}
}
Add a textviewhelloormliteactivity. Java file to the layout file to add database operations. Code As follows:
 Package CN. sdx;

Import Java. SQL. sqlexception;
Import Java. util. List;

Import Com. j256.ormlite. Android. apptools. ormlitebaseactivity;
Import Com. j256.ormlite. Dao. Dao;

Import Android. OS. Bundle;
Import Android. widget. textview;
Import CN. sdx. model. Hello;
Import CN. sdx. utils. datahelper;

Public Class Helloormliteactivity Extends Ormlitebaseactivity <datahelper> {
/** Called when the activity is first created. */
@ Override
Public Void Oncreate (bundle savedinstancestate ){
Super . Oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Textview TV = (textview) This . Findviewbyid (R. Id. output );
Try {
Dao // Add data
For ( Int I = 0; I <2; I ++ ){
Hello = New Hello ("hello" + I );
Hellodao. Create (Hello );
}
TV. settext (TV. gettext () + "\ n" + "added data ");
// Query the added data
List <Hello> hellos = hellodao. queryforall ();
For (Hello H: hellos ){
TV. settext (TV. gettext () + "\ n" + H. tostring ());
}
// Delete the first data
Hellodao. Delete (hellos. Get (0 ));
TV. settext (TV. gettext () + "\ n" + "data deletion completed ");
// Re-query data
Hellos = hellodao. queryforall ();
For (Hello H: hellos ){
TV. settext (TV. gettext () + "\ n" + H. tostring ());
}
// Modify data
Hello H1 = hellos. Get (0 );
H1.setword ("this is modified data ");
TV. settext (TV. gettext () + "\ n" + "data modified ");
Hellodao. Update (H1 );
// Re-query data
Hellos = hellodao. queryforall ();
For (Hello H: hellos ){
TV. settext (TV. gettext () + "\ n" + H. tostring ());
}

} Catch (Sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}

}
}

The above implementation of database operations related to addition, deletion, and modification, the following is the effect:

Ormlite has very powerful functions. The declaration of model classes is very important. Foreign key constraints and non-empty checks all have relative solutions.

Related Article

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.