Persistence using ormlite in Android (1) -- 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.


The implementation considerations are: androrm Official Website: http://androrm.the-pixelpla.net/to put it bluntly, I have no idea about this. I have two packages in total. One is the dependency package: Apache Commons-Lang (2.6) and the other 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: http://ormlite.com/

A total of two packages: one is the ormlite-core-4.24.jar, the other is the ormlite-android-4.24.jar

Download to: http://ormlite.com/releases/ from the following URL

Follow the Convention below to create a Hello world Android project: HelloOrmLite

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

Create a model: Hello. java

Package cn. sdx. model;

 

Import com. j256.ormlite. field. DatabaseField;

 

Public class Hello {

@ DatabaseField (generatedId = true)

Int id;

@ DatabaseField

String word;

 

Public Hello (){

}

 

@ Override

Public String toString (){

StringBuilder sb = new StringBuilder ();

Sb. append ("id ="). append (id );

Sb. append (", word ="). append (word );

Return sb. toString ();

}

 

}

@ DatabaseField: Declares the id as the database field, and generatedId = true declares that the id is auto-incrementing.

Then rewrite toString ()

 

Add another 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 <Hello, Integer> helloDao = null;

 

Public DataHelper (Context context ){

Super (context, DATABASE_NAME, null, DATABASE_VERSION );

}

 

@ Override

Public void onCreate (SQLiteDatabase db, ConnectionSource 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 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 <Hello, Integer> getHelloDataDao () throws SQLException {

If (helloDao = null ){

HelloDao = getDao (Hello. class );

}

Return helloDao;

}

}

 

Add a TextView to the layout File

Add database operations in HelloOrmliteActivity. java

 

The Code is 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 <Hello, Integer> helloDao = getHelper (). getHelloDataDao ();

// Add data

For (int I = 0; I <2; I ++ ){

Hello 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 entry

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 is very powerful, and the declaration of Model classes is very important. Foreign key constraints, non-empty checks, and other issues have relative solutions.


 

Author: Huang yunkun

 

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.