Use ormlite in Android for persistence-HelloOrmLite
By Huang yunkun
Updated:
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: 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
The following is a normal Hello world
Create an Android project: HelloOrmLite
Add Folder: libs, copy the two required packages to it. Add reference
Create a model: Hello. java
12345678910111213141516171819202122 |
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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
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
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
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:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
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
{/** 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
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 () ++ added data); // query the added data List
Hellos = helloDao. queryForAll (); for (Hello h: hellos) {TV. setText (TV. getText () ++ h. toString ();} // Delete the first data helloDao. delete (hellos. get (0); TV. setText (TV. getText () ++ data deletion completed); // query data again hellos = helloDao. queryForAll (); for (Hello h: hellos) {TV. setText (TV. getText () ++ h. toString ();} // modify the data Hello h1 = hellos. get (0); h1.setWord (this is the modified data); TV. setText (TV. getText () ++ completes data modification); helloDao. update (h1); // re-query the data hellos = helloDao. queryForAll (); for (Hello h: hellos) {TV. setText (TV. getText () ++ 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.
Using Ormlite in Android for persistence (2) -- Detailed configuration of persistence classes
By Huang yunkun
Updated:
In the previous article, we used Ormlite in a simple way, but I think Ormlite is better at designing fields, tables, and so on.
The following describes the class Configuration:
If the Android app we developed needs to retain user information, create a new class: UserAccount
This class has six variables:
1 |
private int id; private String username; private String password; private Date regTime; private String tellphone; private String email; |
Use eclipse to generate get and set methods:
1 |
public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Date getRegTime() { return regTime; } public void setRegTime(Date regTime) { this.regTime = regTime; } public String getTellphone() { return tellphone; } public void setTellphone(String tellphone) { this.tellphone = tellphone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } |
The persistence parameters of the class are configured as follows:
1. Table Name:
1 |
@DatabaseTable(tableName=dataTableName) |
If this parameter is not specified, the table name is the class name.
2. Fields
This can be configured with a lot of attributes.
2.1 primary key
1 |
@DatabaseField(id=true) |
2.2 column name
1 |
@DatabaseField(columnName=columnName) |
If this parameter is not specified, it is the same as the variable name.
2.3 Data Type
1 |
@DatabaseField(dataType=DataType.INTEGER) |
In general, you do not need to specify this parameter. You can obtain this parameter based on the java class.
2.4 Default Value
1 |
@DatabaseField(defaultValue=0) |
2.5 Length
1 |
@DatabaseField(width=13) |
It is generally used for String type
2.6 empty
1 |
@DatabaseField(canBeNull=false) |
The default value is True.
2.7 self-growth?
1 |
@DatabaseField(generatedId=true) |
This is relatively simple. Let's talk about the foreign key in the next article.