SQLite is built into Android, but the common development language Java is Object-oriented, and the database is relational, and the conversion between the two is cumbersome every time. (as programmers, should learn to be lazy) and Java Web Development There are many ORM framework (actually I did not learn the Java Web), but want to put on Android directly with some trouble. Try to find the Android ORM Framework, and then found the ormlite.
First go to the official website to learn about ormlite:http://ormlite.com/.
Then download two jar packages, one is Ormlite-core-4.48.jar and the other is Ormlite-android-4.48.jar. (Of course, you can also download the corresponding document) as: http://ormlite.com/releases/.
Then do a simple implementation of the operation (to achieve a database of the creation and additions and deletions to check and change).
Create a new project named "Zhj_ormlite" with the package named "Com.topcsa.zhj_ormlite" and select SDK version 14, Add Ormlite-core-4.48.jar and Ormlite-android-4.48.jar to the project.
Create an entity class person, which is the table that needs to be created.
The @databasetable of the specified class in the code indicates that the corresponding field name @DatabaseField
PackageCom.topcsa.zhj_ormlite;ImportCom.j256.ormlite.field.DatabaseField;Importcom.j256.ormlite.table.DatabaseTable; @DatabaseTable (TableName= "Tb_person") Public classPerson { PublicPerson () {} PublicPerson (String name,intAge ) { Super(); This. Name =name; This. Age =Age ; } @DatabaseField (Generatedid=true)//Generatedid represents the ID as the primary key and is automatically generated Private intID; @DatabaseField (ColumnName= "Name") PrivateString name; @DatabaseField (ColumnName= "Age") Private intAge ; Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; }}
Create a database helper class that inherits Ormlitesqliteopenhelper implementations OnCreate and Onupgrade, similar to the Sqliteopenhelper method of Android, You should be aware of the role of this class, and create a method that deletes a database using the Tableutils tool class.
To create the table, we directly use the Tableutils.createtable (Connectionsource, Person.class) provided by Ormlite;
Update the table, using the tableutils.droptable provided by Ormlite (Connectionsource, Person.class, true);
PackageCom.topcsa.zhj_ormlite;ImportAndroid.content.Context;Importandroid.database.sqlite.SQLiteDatabase;ImportAndroid.util.Log;ImportCom.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;ImportCom.j256.ormlite.dao.Dao;ImportCom.j256.ormlite.support.ConnectionSource;Importcom.j256.ormlite.table.TableUtils; Public classDBHelperextendsOrmlitesqliteopenhelper {Private Static FinalString table_name = "Ormlite.db"; Private StaticDBHelper instance; PublicDBHelper (Context context) {Super(Context, TABLE_NAME,NULL, 1); //TODO auto-generated Constructor stub } /*** Single Instance gets the helper * *@paramContext *@return */ Public Static synchronizedDBHelper Gethelper (context context) {if(Instance = =NULL) { synchronized(DBHelper.class) { if(Instance = =NULL) Instance=NewDBHelper (context); } } returninstance; } @Override Public voidonCreate (sqlitedatabase database, Connectionsource connectionsource) {Try{tableutils.createtable (Connectionsource, person.class); LOG.D ("DBHelper", "CREATE Table succeeded"); } Catch(java.sql.SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }} @Override Public voidOnupgrade (sqlitedatabase database, Connectionsource Connectionsource,intOldversion,intnewversion) { Try{tableutils.droptable (Connectionsource, person.class,true); } Catch(java.sql.SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } onCreate (database, Connectionsource); }}
Create a DAO class and set up Persondao.java.
PackageCom.topcsa.zhj_ormlite;Importjava.sql.SQLException;Importjava.util.List;ImportAndroid.content.Context;ImportAndroid.util.Log;ImportCom.j256.ormlite.dao.Dao; Public classPersondao {Context con; PrivateDBHelper Helper; PrivateDao<person, integer>Persondao; //constructor Function PublicPersondao (Context con) {helper=dbhelper.gethelper (con); This. con =con; } //Each table generally we will write a DAO for Operation PublicDao<person, integer> Getpersondao ()throwsjava.sql.SQLException {if(Persondao = =NULL) {Persondao= Helper.getdao (person.class); } returnPersondao; } //Query by ID PublicPerson Selectperson (inti) {Try{person P=Getpersondao (). Queryforid (i); LOG.D ("TAG", "Query ID for" + i + "man:" +p.getname ()); returnp; } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } return NULL; } //Add someone Public voidAddperson (person p) {Try{Getpersondao (). Create (P); LOG.D ("TAG", "Add:" +p.getname ()); } Catch(SQLException e) {}}//Remove Someone Public voidDeleteperson (person p) {Try{LOG.D ("TAG", "Remove ID for" + p.getid () + "Person:" +p.getname ()); Getpersondao (). Deletebyid (P.getid ()); } Catch(SQLException e) {}}//update SB . Public voidUpdateperson (person p, String name) {Try{LOG.D ("TAG", "before modifying Data name:" +p.getname ()); P.setname (name); Getpersondao (). Update (P); LOG.D ("TAG", "after modifying the data name:" +p.getname ()); } Catch(SQLException e) {}}//Query Everyone PublicList<person>Showpersonall () {Try{List<Person> list =Getpersondao (). Queryforall (); LOG.D ("TAG", "Query all data Bar number:" +list.size ()); for(inti = 0; I < list.size (); i++) {LOG.D ("TAG", "Single Data name:" +List.get (i). GetName ()); } returnlist; } Catch(SQLException e) {}return NULL; }}
Finally, in the activity of the specific implementation:
PackageCom.topcsa.zhj_ormlite;Importandroid.app.Activity;ImportAndroid.os.Bundle; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Person P=NewPerson ("Zhao", 21); Persondao Pdao=NewPersondao ( This); Try{Pdao.addperson (P); P=NewPerson ("Huang Qiu", 21); Pdao.addperson (P); P=NewPerson ("What rights", 21); Pdao.addperson (P); P=NewPerson ("Ningui", 21); Pdao.addperson (P); P=NewPerson ("Zhongjun", 21); Pdao.addperson (P); Pdao.showpersonall (); System.out. println ("-------------------------------the king of the Land Tiger------------------------------"); Person P1= Pdao.selectperson (2); Pdao.updateperson (P1,"Sew Sew ~ ~ ~ ~ ~ ~"); P= Pdao.selectperson (1); Pdao.deleteperson (P); Pdao.showpersonall (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
The results of the operation are as follows:
Android Ormlite for easy use of data persistence