Use ormlite for persistence in Android

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 downloaded... next, let's take a look at the db4o Official Website: The ormlite official website was last seen by ghost: follow the Convention 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 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 <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 ;}}
The following code adds TextViewHelloOrmliteActivity. java to the layout file:
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 helloDao. delete (hellos. get (0); TV. setText (TV. getText () + "\ n" + ""); // query the data again. hellos = helloDao. queryForAll (); for (Hello h: hellos) {TV. setText (TV. getText () + "\ n" + h. toString ();} // modify the data Hello h1 = hellos. get (0); h1.setWord ("this is modified data"); TV. setText (TV. getText () + "\ n" + "data modified"); helloDao. update (h1); // re-query the 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. Author: huangyunkun

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.