<span id="Label3"></p><pre>/*----------------------------------------<br>*-describes the--ormlite Framework Usage summary.<br>*-description--s1. Related theory Description<br>* >>ormlite with Java Annotations to establish a mapping relationship with the database, in the android-oriented database development, is a comparison<br>* Popular Open source framework, easy to operate and powerful<br>* Advantages and disadvantages of >>ormlite<br>* Advantages: lightweight, easy to use, well packaged, comprehensive documentation<br>* Cons: based on reflection, low efficiency, lack of Chinese translation documents<br>* >><br>*-description--s2. Add to Project<br>* >> Add library file to project libs, add as Library<br>* ormlite-android-5.0.jar, Ormlite-android-5.0-sources.jar<br>* ormlite-core-5.0, Ormlite-core-5.0-sources.jar<br>* >> inherit Ormlitesqliteopenhelper class Definition database helper class (xxxhelper)<br>* The database can be created or upgraded when the program is installed<br>* OnCreate (sqlitedatabase sqlitedatabase, connectionsource connectionsource)<br>* Onupgrade (sqlitedatabase database, connectionsource connectionsource, int oldversion, int Newversion)<br>* >> define entity class bean, representing a table (xxxmodel)<br>* @DatabaseTable: indicates that a data table is defined, and if you do not specify a name, the name of the table will be in ANDROID.<br>* If PackageInfo is the name of a table in the SQLite database, we can also specify the table name, @DatabaseTable (tableName =<br>* "tb_package")<br>* @DatabaseField: indicates that a field in the data is defined, the ID represents a primary key in the data, and if specified as generatedid,<br>* Indicates an auto-grow id, and we don't need to assign a value to it. Other fields, you can use ColumnName to specify the field name, canbenull table<br>* Indicates whether null, these assignments can be specified as Follows-(id = true, Canbenull = False) (columnName = "name")<br>* @DatabaseField Common Notes:<br>* ColumnName Specifies the field name, without specifying the variable name as the field name<br>* Whether the Canbenull field can be assigned a null Value. The default is True. If you set it to false, then each time you are in the database<br>* Insert data is required to provide a value for this FIELD.<br>* DataType The type of the specified field<br>* DefaultValue The default value of a field when we create a new record in a table. By default, this value is not<br>* Width field, which is used primarily for string fields. Default is 0<br>* ID This field is id, default is False. There is only one variable in a class that can have this Value. The ID field is a<br>* The unique identification of the record and is required, can only be selected in Generatedid and Generatedidsequence.<br>* The Generatedid field is automatically Incremented. The default is False. A member variable in a class sets this value, which tells the database<br>* The ID is automatically incremented for each new record Added. Use the Dao.create () method when an object with Generatedid is created<br>*, the database will generate an ID for the record, which will be returned and set into the object by the Create Method.<br>* Generatedidsequence Serial number name, This value will be used when Generated. Similar to generatedid, but<br>* You can specify the sequence name to Use. The default is No.<br>* foreign specifies that the object of this field is a foreign key, the foreign key value is the ID of this object<br>* persisted Specifies whether this variable is persisted, by default true<br>* If the foreignautocreate foreign key does not exist, it is automatically added to the external table<br>* Foreigncolumnname field in the foreign key table specified by the Foreign key field<br>* @ForeignCollectionField frequently used annotations<br>* Eager indicates whether the collection is to be removed when the object is initialized or not, and the default is False<br>* When traversing, the size () method will also cause traversal<br>* ColumnName<br>* Ordercolumnname<br>* Foreignfieldname<br>* >> database changes and additions (xxxdao)<br>* Insert operation<br>* Create: Insert a piece of data<br>* Createifnotexists: Insert if not present<br>* createorupdate: Update if present<br>* Query operation<br>* First Call Xxxdao.querybuilder (); get the QueryBuilder object under the DAO<br>* Next set the QueryBuilder query condition<br>* Finally get the list object by Querybuilder's Query method<br>* Delete and change actions<br>* Similar to query operations, Ormlite also provides us with a range of methods, as well as complex deletions, complex<br>* Modified Deletebuilder and updatebuilder, which are similar in usage to QueryBuilder<br>* Main methods<br>* Create inserts a piece of data<br>* Createifnotexists Insert if not present<br>* Createorupdate If the specified ID is updated<br>* Queryforid More ID Lookup<br>* Update finds out data<br>* Delete Deletes data<br>* QueryBuilder () Create a query builder: make complex queries<br>* Deletebuilder () Create a delete generator, process complex condition Delete<br>* Updatebuilder () Create a fix condition generator for complex condition modification<br>* Reference: https://www.zybuluo.com/flyouting/note/6996<br>*-description--deep section<br>* Ormlite FOREIGN Key reference<br>* Associate a Collection<br>Reference<br>* http://blog.csdn.net/lmj623565791/article/details/39122981<br>* HTTP://WWW.JIANSHU.COM/P/05782B598CF0<br>* https://bng86.gitbooks.io/android-third-party-/content/ormlite.html<br>*-describe--b1.none.<br>* Official Website:<br>* http://ormlite.com/<br>* http://ormlite.com/releases/<br>* Https://github.com/j256/ormlite-android<br>* Reference Documents:<br>* http://blog.csdn.net/lmj623565791/article/details/39121377<br>* http://blog.csdn.net/lmj623565791/article/details/39122981<br>* HTTP://WWW.JIANSHU.COM/P/05782B598CF0<br>* https://bng86.gitbooks.io/android-third-party-/content/ormlite.html<br>*---------------------------------------------------------------------------*/</pre><pre>/**<br>* Created by ENVY15 on 2016/8/2.<br>* Create database table entries<br>* For each member variable that needs to be stored in the class, you need to add @databasefield annotations<br>*/<br>TableName represents the table name that created the table, without which the name of the table is used<br>@DatabaseTable (tableName = "model_table")<br>public class Testmodel implements Serializable {<br>Private final String test_model_id = "ID";<br>Private final String Test_model_title = "modeltitle";<br>Private final String Test_model_url = "modelurl";<br>Private final String Test_model_detail = "modeldetail";<br>Private final String Test_model_count = "modelcount";<br>Private final String test_model_state = "modelstatus";<br>Private final String Test_model_item = "modelItem";<br>ID = True To configure primary key, Generatedid = True primary key Auto-grow<br>@DatabaseField (generatedid = True)<br>public int id;<br>Add @databasefield annotations to stored member variables<br>@DatabaseField (columnName = Test_model_title)<br>Public String modeltitle;<br>@DatabaseField (columnName = Test_model_url)<br>Public String modelurl;<br>@DatabaseField (columnName = Test_model_detail)<br>Public String modeldetail;<br>@DatabaseField (columnName = Test_model_count)<br>public int modelcount;<br>@DatabaseField (columnName = Test_model_state,defaultvalue = "0")<br>public int modelstatus;<br>Canbenull-indicates that a foreign key cannot be represented as null;foreign=true; columnName column name<br>@DatabaseField (canbenull = true, Foreign = true, ColumnName = Test_model_item)<br>Private TestItem testitem;<br><br>The following comments are not debugged<br>@ForeignCollectionField indicates that the data in this table is a foreign key in the other table<br>The type of the member variable items must either be foreigncollection<t> or collection<t><br>@ForeignCollectionField (eager = False)<br>Public foreigncollection<testitem> items;<br><br>External Object fields<br>@DatabaseField (foreign = true, Foreignautorefresh = True)<br>Public PackageInfo mpackage;<br>//<br>@DatabaseField (foreign = true, Foreignautorefresh = True)<br>Public photographer photographer;<br>// /**<br>* Note here: The attribute type can only be foreigncollection<t> or collection<t><br>* If lazy loading (deferred Loading) is required, @foreigncollectionfield parameters can be added Eager=false<br>* This attribute also indicates that a department corresponds to multiple users<br>// */<br>@ForeignCollectionField (eager = True)<br>Public foreigncollection imgs;<br><br><br>Public Testmodel () {<br>}<br><br>Public Testmodel (string modeltitle, string Modeldetail) {<br>This.modeltitle = modeltitle;<br>This.modeldetail = modeldetail;<br>}<br><br>Public Testmodel (string modeltitle, string modeldetail, int Modelstatus) {<br>This.modeltitle = modeltitle;<br>This.modeldetail = modeldetail;<br>This.modelstatus = modelstatus;<br>}<br><br>public int Getmodelid () {<br>Return id;<br>}<br><br>public void Setmodelid (int Modelid) {<br>This.id = modelid;<br>}<br><br>Public String Getmodeltitle () {<br>Return modeltitle;<br>}<br><br>public void Setmodeltitle (String Modeltitle) {<br>This.modeltitle = modeltitle;<br>}<br><br>Public String Getmodelurl () {<br>Return modelurl;<br>}<br><br>public void Setmodelurl (String Modelurl) {<br>This.modelurl = modelurl;<br>}<br><br>Public String Getmodeldetail () {<br>Return modeldetail;<br>}<br><br>public void Setmodeldetail (String Modeldetail) {<br>This.modeldetail = modeldetail;<br>}<br><br>public int Getmodelcount () {<br>Return modelcount;<br>}<br><br>public void Setmodelcount (int Modelcount) {<br>This.modelcount = modelcount;<br>}<br><br>public int Getmodelstatus () {<br>Return modelstatus;<br>}<br><br>public void Setmodelstatus (int Modelstatus) {<br>This.modelstatus = modelstatus;<br>}<br><br>Public TestItem Gettestitem () {<br>Return testitem;<br>}<br><br>public void Settestitem (testitem Testitem) {<br>This.testitem = testitem;<br>}<br><br>@Override<br>Public String toString () {<br>return "testmodel{" +<br>"id=" + ID +<br>", modeltitle= '" + modeltitle + "+<br>", modelurl= '" + modelurl + "+<br>", modeldetail= '" + modeldetail + "+<br>", modelcount=" + Modelcount +<br>", modelstatus=" + modelstatus +<br>‘}‘;<br>}<br>}<br><br></pre><pre>/**<br>* Created by ENVY15 on 2016/8/2.<br>* Define the data Access object and make a deletion and check operation on the specified table.<br>*<br>*/<br>public class Modeldao {<br>Private dao<testmodel, integer> modeldao;<br>Private DBHelper dbhelper;<br><br>/**<br>* Construction Method<br>* Get the database helper class instance and get the corresponding DAO by passing in class object<br>* @param context<br>*/<br>Public Modeldao (context Context) {<br>try {<br>DBHelper = Dbhelper.gethelper (context);<br>Modeldao = Dbhelper.getdao (testmodel.class);<br>} catch (SQLException E) {<br>E.printstacktrace ();<br>}<br>}<br><br>/**<br>* Add a record<br>* @param model<br>*/<br>public void Add (testmodel Model) {<br>try {<br>Logutil.infoe (this, "model:" + (model==null) + "modeldao:" + (modeldao==null));<br>Modeldao.create (model);<br>} catch (SQLException E) {<br>E.printstacktrace ();<br>}<br>}<br><br>/**<br>* Delete a record<br>* @param model<br>*/<br>public void Delete (testmodel Model) {<br>try {<br>Modeldao.delete (model);<br>} catch (SQLException E) {<br>E.printstacktrace ();<br>}<br>}<br><br><br>/**<br>* Update a record<br>* @param model<br>*/<br>public void Update (testmodel Model) {<br>try {<br>Modeldao.update (model);<br>} catch (SQLException E) {<br>E.printstacktrace ();<br>}<br>}<br><br>/**<br>* Query a record<br>* @param ID<br>* @return<br>*/<br>Public Testmodel Queryforid (int Id) {<br>Testmodel model = null;<br>try {<br>Model = Modeldao.queryforid (id);<br>} catch (SQLException E) {<br>E.printstacktrace ();<br>}<br>Return model;<br>}<br><br>/**<br>* Query matching records according to the model<br>* @param model<br>* @return<br>*/<br>Public list<testmodel> Queryformodel (testmodel Model) {<br>list<testmodel> Mmodel = null;<br>try {<br>Mmodel = modeldao.queryformatching (model);<br>Queryformatching/queryformatchingargs Query Results as<br>Mmodel = Modeldao.queryformatchingargs (model);<br>} catch (SQLException E) {<br>E.printstacktrace ();<br>}<br>Return mmodel;<br>}<br><br>/**<br>* Query matching records based on model Key-value<br>* @param var1 Key<br>* @param var2 value<br>* @return<br>*/<br>Public list<testmodel> queryforkey (String var1, Object var2) {<br>list<testmodel> Mmodel = null;<br>try {<br>Mmodel = Modeldao.queryforeq (var1,var2);<br>} catch (SQLException E) {<br>E.printstacktrace ();<br>}<br>Return mmodel;<br>}<br><br>/**<br>* Other Query Test<br>* @param model<br>* @return<br>Reference<br>* http://blog.csdn.net/quanjin24k/article/details/11620803<br>* http://www.cnblogs.com/vanezkw/archive/2012/08/15/2640290.html<br>* Http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_2.html#Class-Setup<br>* http://www.ithao123.cn/content-8115868.html<br>*/<br>Public list<testmodel> QueryForModel1 (map<string, object> MMap) {<br>Public list<testmodel> QueryForModel1 (testmodel Model) {<br>list<testmodel> Mmodel = null;<br>try {<br>Based on model query<br>Mmodel = modeldao.queryformatching (model);<br>Query by Key-value<br>Mmodel = Modeldao.queryforeq ("modeltitle", "setmodeltitle");<br>Mmodel = Modeldao.queryforeq ("modelstatus", 3);<br>Query based on multiple key-value parameters (map Collection)<br>map<string, object> mMap = new hashmap<string, object> ();<br>Mmap.put ("modelstatus", 3);<br>Mmap.put ("modeltitle", "setmodeltitle");<br>Mmodel = Modeldao.queryforfieldvalues (mMap);<br>Mmodel = Modeldao.queryforfieldvaluesargs (mMap);<br>Query with a sort<br>Mmodel = Modeldao.querybuilder (). ("id", false). where (). eq ("modelstatus", 3)<br>. and (). eq ("modeltitle", "setmodeltitle"). Query ();<br>Sorting query results with SQL statements<br>Mmodel = Modeldao.querybuilder (). orderbyraw ("sql-statement"). where (). eq ("modelstatus", 3)<br>. and (). eq ("modeltitle", "setmodeltitle"). Query ();<br>SQL statement Query<br>Modeldao.queryraw ("select * FROM data where id = 2");<br>} catch (SQLException E) {<br>E.printstacktrace ();<br>}<br>Return mmodel;<br>}<br><br>/**<br>* Check all records<br>* @return<br>*/<br>Public list<testmodel> Queryforall () {<br>list<testmodel> models = new Arraylist<testmodel> ();<br>try {<br>Models = Modeldao.queryforall ();<br>} catch (SQLException E) {<br>E.printstacktrace ();<br>}<br>Return models;<br>}<br>}<br><br></pre><pre>/**<br>* Created by ENVY15 on 2016/8/2.<br>* The database can be created or upgraded when the program is installed<br>* Required: one for the build database, one for the database upgrade<br>* OnCreate (sqlitedatabase sqlitedatabase, connectionsource connectionsource)<br>* Onupgrade (sqlitedatabase database, connectionsource connectionsource, int oldversion, int Newversion)<br>*/<br>public class DBHelper extends Ormlitesqliteopenhelper {<br>/**<br>* Database name<br>*/<br>private static final String db_name = "testmodel.db";<br>/**<br>* Database version<br>*/<br>private static final int db_version = 1;<br>/**<br>* A map to store DAO<br>*/<br>Private map<string, dao> Daos = new Hashmap<> ();<br><br>private static DBHelper instance;<br><br>/**<br>* Construction Method<br>* @param context<br>*/<br>Public DBHelper (context Context) {<br>Super (context, db_name, null, db_version);<br>}<br><br>/**<br>* Create a table here<br>*/<br>@Override<br>public void OnCreate (sqlitedatabase sqlitedatabase, connectionsource Connectionsource) {<br>Create a table<br>try {<br>Tableutils.createtable (connectionsource, testmodel.class);<br>} catch (SQLException E) {<br>E.printstacktrace ();<br>}<br>}<br><br>/**<br>* Update table operation is performed here<br>*/<br>@Override<br>public void Onupgrade (sqlitedatabase sqlitedatabase, connectionsource connectionsource,<br>int oldversion, int Newversion) {<br>Try<br>{<br>Tableutils.droptable (connectionsource, testmodel.class, true);<br>OnCreate (sqlitedatabase, connectionsource);<br>} catch (SQLException E)<br>{<br>E.printstacktrace ();<br>}<br>}<br><br>/**<br>* Get a single case<br>* @param context<br>* @return<br>*/<br>public static synchronized DBHelper Gethelper (context Context) {<br>context = Context.getapplicationcontext ();<br>if (instance = = Null) {<br>Synchronized (dbhelper.class) {<br>if (instance = = Null) {<br>Instance = new DBHelper (context);<br>}<br>}<br>}<br>Return instance;<br>}<br>/**<br>* Get a singleton-database storage SD directory<br>* @param context<br>* @return<br>*/<br>public static synchronized DBHelper gethelper (Context context,string Path) {<br>context = new Databasecontext (context, path);<br>if (instance = = Null) {<br>Synchronized (dbhelper.class) {<br>if (instance = = Null) {<br>Instance = new DBHelper (context);<br>}<br>}<br>}<br>Return instance;<br>}<br><br>/**<br>* Use class to get the specified DAO<br>*/<br>Public synchronized Dao Getdao (Class Clazz) throws SQLException<br>{<br>DAO DAO = null;<br>String className = Clazz.getsimplename ();<br><br>If (daos.containskey (className)) {<br>DAO = Daos.get (className);<br>}<br>if (dao = = Null) {<br>DAO = Super.getdao (clazz);<br>Daos.put (className, dao);<br>}<br>Return dao;<br>}<br><br><br>/**<br>* Release Resources<br>*/<br>@Override<br>public void Close () {<br>Super.close ();<br>For (String Key:daos.keySet ()) {<br>Dao DAO = Daos.get (key);<br>DAO = null;<br>}<br>}<br>}<br><br></pre><pre>public void Initdatas () {//test Program<br>Logutil.infod (this, "initviews");<br>Logutil.infoe (this, "ormlitestarttime:" +system.currenttimemillis () + "");<br><br>Testmodel testModel1 = new Testmodel ("setmodeltitle", "setModelDetail1", 1);<br>Testmodel TestModel2 = new Testmodel ("setmodeltitle", "setModelDetail2", 2);<br>Testmodel testModel3 = new Testmodel ("setmodeltitle", "setModelDetail3", 3);<br>Testmodel testModel4 = new Testmodel ("setmodeltitle", "setModelDetail4", 4);<br>Testmodel testModel5 = new Testmodel ("setmodeltitle", "setModelDetail5", 5);<br>Modeldao Modeldao = new Modeldao (this);<br>Logutil.infoe (this, "testmodel:" + (testmodel==null) + "modeldao:" + (modeldao==null));<br>Insert operation test for database<br>Modeldao.add (testModel1);<br>Modeldao.add (testModel2);<br>Modeldao.add (testModel3);<br>Modeldao.add (testModel4);<br>Modeldao.add (testModel5);<br><br>Query a record (based on the Id) and store it in the database after the change<br>Testmodel = Modeldao.queryforid (5);<br>Testmodel.setmodelstatus (8);<br>Modeldao.update (testmodel);<br><br>Delete operation, Delete is deleted by Testmodel ID<br>Testmodel.setmodelid (6);<br>Modeldao.delete (testmodel);<br><br>Query operation test of database<br>list<testmodel> testmodellist = Modeldao.queryforall ();<br>For (int i=0;i<testmodellist.size (); I++) {<br>Logutil.infoe (this, testmodellist.get (i). toString ());<br>}<br><br>Conditional query, According to Testmodel parameter matching, Testmodel set several parameters to match several parameters<br>Testmodel testModel6 = new Testmodel ();<br>Testmodel6.setmodeltitle ("setmodeltitle");<br>list<testmodel> TestModelList1 = Modeldao.queryformodel1 (testModel6);<br>For (int i=0;i<testmodellist1.size (); I++) {<br>Logutil.infoe (this, "conditional query" +testmodellist1.get (i). toString ());<br>}<br>Logutil.infoe (this, "ormlitestartend:" +system.currenttimemillis () + "");<br>}</pre><p><p>Android Database Ormlite Framework Summary</p></p></span>
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