Introduction to the Android ormlite framework

Source: Internet
Author: User

1. ormlite framework
1. Download the core package coreand android libraries from http://ormlite.com/releases/and then add two jarpackages to the project.
2. Stored data object entity
Public class Entity {
@ DatabaseField (generatedId = true) // The auto-increment primary key.
Int id;
@ DatabaseField // declare string as a database Field
String string;
Public Entity (){
// There must be a non-argument constructor in ormlite
}
...
}
Ormlite is used to configure the persistence parameters of the class through annotation. The common parameters are as follows:
1. Table Name: if not specified, the table name is the class name.
@ DatabaseTable (tableName = "dataTableName ")
2. Field: There are many properties that can be configured.
@ DatabaseField
(1) primary key:
@ DatabaseField (id = true)
(2) column name: If this parameter is not specified, it is the same as the variable name.
@ DatabaseField (columnName = "columnName ")
(3) Data Type: Generally, you do not need to specify the data type, which can be obtained according to the java class.
@ DatabaseField (dataType = DataType. INTEGER)
(4) default value:
@ DatabaseField (defaultValue = "0 ")
(5) Length: generally used for String type
@ DatabaseField (width = 13)
(6) can it be blank? The default value is True.
@ DatabaseField (canBeNull = false)
3. The data DataHelper class is required to create and manage the database. The DataHelper class inherits OrmLiteSqliteOpenHelper. The onCreate, onUpgrade, and close methods are implemented in overwriting.
@ Override
Public void onCreate (SQLiteDatabase db, ConnectionSource connectionSource ){
Try {
Log. e (DataHelper. class. getName (), "Start database creation ");
TableUtils. createTable (connectionSource, Entity. class );
Log. e (DataHelper. class. getName (), "database created successfully ");
} Catch (SQLException e ){
Log. e (DataHelper. class. getName (), "failed to create database", e );
}
}
@ Override
Public void onUpgrade (SQLiteDatabase db, ConnectionSource connectionSource, int arg2, int arg3 ){
Try {
TableUtils. dropTable (connectionSource, Entity. class, true );
OnCreate (db, connectionSource );
Log. e (DataHelper. class. getName (), "database updated ");
} Catch (SQLException e ){
Log. e (DataHelper. class. getName (), "failed to update database", e );
}
}
@ Override
Public void close (){
Super. close ();
}
4. The corresponding data Dao class is required, that is, the data access interface.
Public class EntityDao {
Public List findData (Dao Entitydao, int id) throws SQLException {
HashMap EntityMap = new HashMap ();
UserguideMap. put ("id", id );
List EntityLists = entityDao. queryForFieldValues (EntityMap );
Return EntityLists = null? Null: EntityLists;
}
Public void addEntity (Dao entityDao, String string) throws SQLException {
Entity = new Entity (string );
EntityDao. create (Entity );
}
}
5. Call
Ormlite can be used in two ways. One is to inherit the corresponding OrmLiteBaseActivity. but like xxxx Activity itself must inherit BaseActivity, so you must use another method: Add a method to activity to get helper, and add a method to onDestroy to close helper in time.
Private DataHelper dataHelper = null;
Private DataHelper getHelper (){
If (dataHelper = null ){
DataHelper = OpenHelperManager. getHelper (this, DataHelper. class );
}
Return dataHelper;
}
@ Override
Protected void onDestroy (){
// TODO Auto-generated method stub
Super. onDestroy ();
If (dataHelper! = Null ){
OpenHelperManager. releaseHelper ();
DataHelper = null;
}
}
The database operation implementation code is as follows:
EntityDao dao = new EntityDao ();
Try {
Dao dataDao = getHelper (). getDataDao ();
// Query operation call
List simpledataList = dao. findData (dataDao, 1 );
// Add operation call
Dao. addEntity (dataDao, "demotest ");
} Catch (SQLException e ){
E. printStackTrace ();

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.