<span id="Label3"></p>Objective<p><p>In Android development, data needs to be stored locally, and SQLite is of course used when a series of operations are Required. The standard practice is to inherit sqliteopenhelper, overriding the OnCreate and OnUpdate methods. The big problem is that SQL statements need to be written on their own, more complicated and repetitive, and then find an ORM framework to try out in the newly written app.</p></p>Get ready<p><p>: http://ormlite.com/releases/<br>Download the core and Android two jar packages and bring them to the project</p></p>Development model Annotations<p><p>First define one of the most common personnel models</p></p><pre class="prettyprint"><code class="language-java hljs "><span class="hljs-comment"><span class="hljs-comment">//tablename define table name, default to class name</span></span><span class="hljs-annotation"><span class="hljs-annotation">@DatabaseTable</span></span>(tableName =<span class="hljs-string"><span class="hljs-string">"table_person"</span></span>)<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title"></span>person {</span></span> <span class="hljs-annotation"><span class="hljs-annotation">@DatabaseField</span></span>(generatedid =<span class="hljs-keyword"><span class="hljs-keyword">true</span></span>)<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Id<span class="hljs-comment"><span class="hljs-comment">//columnname define field names, default to variable names</span></span> <span class="hljs-annotation"><span class="hljs-annotation">@DatabaseField</span></span>(columnName =<span class="hljs-string"><span class="hljs-string">"first_name"</span></span>)<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>String firtname;<span class="hljs-annotation"><span class="hljs-annotation">@DatabaseField</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>String lastName;<span class="hljs-comment"><span class="hljs-comment">//need to define an parameterless constructor, Ormlite used to construct objects with reflection when querying</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-title"><span class="hljs-title"></span> person</span>() { }<span class="hljs-comment"><span class="hljs-comment">//other Constructors</span> ...</span> <span class="hljs-comment"><span class="hljs-comment">//getters and Setters</span> ...</span>}</code></pre><p><p>The @DatabaseField annotation fields are described below:<br>Original website</p></p> <table> <thead> <tr> <th>name</th> <th>Data Type</th> <th>Default Value</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td>ColumnName</td> <td>String</td> <td></td> <td>Field name, default is variable name</td> </tr> <tr> <td>DataType</td> <td>String</td> <td></td> <td>Set datatype is the field type, usually not specified, corresponding to the SQL type</td> </tr> <tr> <td>DefaultValue</td> <td>String</td> <td>Empty</td> <td>Default value, default is empty</td> </tr> <tr> <td>Width</td> <td>Integer</td> <td></td> <td>Typically used in string fields. The default is 0, which means the default Length. String Default 255</td> </tr> <tr> <td>Canbenull</td> <td>Boolean</td> <td>True</td> <td>Whether or not to empty</td> </tr> <tr> <td>Id</td> <td>Boolean</td> <td>False</td> <td>Specifies whether the field is an id, and a class can have only one domain setting this</td> </tr> <tr> <td>Generatedid</td> <td>Boolean</td> <td>False</td> <td>If the domain is an auto-generated ID field, a class can have only one domain set for this</td> </tr> <tr> <td>Generatedidsequence</td> <td>String</td> <td></td> <td>Specifies the Sequence. Starting from 1, 1 per increment</td> </tr> <tr> <td>Foreign</td> <td>Boolean</td> <td>False</td> <td>Specifies whether the field corresponds to another class, and the other class must have an ID field</td> </tr> <tr> <td>Usegetset</td> <td>Boolean</td> <td>False</td> <td>Whether access is required through the get and set methods, through Java reflection</td> </tr> <tr> <td>Unknownenumname</td> <td>String</td> <td></td> <td>If the field is a Java enumeration type, you can specify an enumeration value that is used when the value of the database row is not in the enumeration type.</td> </tr> <tr> <td>Throwifnull</td> <td>Boolean</td> <td>False</td> <td>Ormlite throws an exception when the field is empty for insertion</td> </tr> <tr> <td>Persisted</td> <td>Boolean</td> <td>True</td> <td>When set to false, the database is not stored</td> </tr> <tr> <td>Format</td> <td></td> <td></td> <td></td> </tr> <tr> <td>Unique</td> <td>Boolean</td> <td>False</td> <td>Column Uniqueness</td> </tr> <tr> <td>Uniquecombo</td> <td>Boolean</td> <td>False</td> <td>Union Column uniqueness</td> </tr> <tr> <td>Index</td> <td>Boolean</td> <td>False</td> <td>index, index name implicitly considers column name plus suffix _idx</td> </tr> <tr> <td>Uniqueindex</td> <td>Boolean</td> <td>False</td> <td>Creates a unique index in which all values in this index are unique</td> </tr> <tr> <td>IndexName</td> <td>String</td> <td>None</td> <td>Specifies the index name, you do not need to specify an index Boolean value separately. In order to index multiple domains in an index, each domain needs to have the same indexname</td> </tr> <tr> <td>Uniqueindexname</td> <td>String</td> <td>None</td> <td>Specifies a unique index name in which all values in this index are unique</td> </tr> <tr> <td>Foreignautorefresh</td> <td>Boolean</td> <td>False</td> <td>Foreign key Auto Refresh</td> </tr> <tr> <td>Maxforeignautorefreshlevel</td> <td></td> <td></td> <td></td> </tr> <tr> <td>Allowgeneratedidinsert</td> <td>Boolean</td> <td>False</td> <td>If set to true, ID field overrides are not automatically generated when an object that contains an ID field is inserted</td> </tr> <tr> <td>ColumnDefinition</td> <td></td> <td></td> <td></td> </tr> <tr> <td>Foreignautocreate</td> <td></td> <td></td> <td></td> </tr> <tr> <td>Version</td> <td>Boolean</td> <td>False</td> <td></td> </tr> <tr> <td>Foreigncolumnname</td> <td>String</td> <td></td> <td>Specify foreign Key Object association fields</td> </tr> </tbody> </table>Complete Helper<p><p>Similar to native sqliteopenhelper, the use of Ormlite requires inheriting ormlitesqliteopenhelper, as Follows:</p></p><pre class="prettyprint"><code class="language-java hljs "><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">dbhelper</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">ormlitesqliteopenhelper</span> {</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-title"><span class="hljs-title">DBHelper</span></span>(context Context) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>(context, table_name,<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>, VERSION); }<span class="hljs-comment"><span class="hljs-comment">//create table is very simple here, using the tableutils createtable method, you do not need to write SQL statements such as CREATE TABLE and so On. </span></span> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>(sqlitedatabase database, Connectionsource Connectionsource) {<span class="hljs-keyword"><span class="hljs-keyword">Try</span></span>{tableutils.createtable (connectionsource, person.class); }<span class="hljs-keyword"><span class="hljs-keyword">Catch</span></span>(SQLException E) {e.printstacktrace (); } }<span class="hljs-comment"><span class="hljs-comment">//override OnUpdate Method</span></span> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Onupgrade</span></span>(sqlitedatabase database, Connectionsource connectionsource,<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>oldversion,<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Newversion) {<span class="hljs-keyword"><span class="hljs-keyword">Try</span></span>{<span class="hljs-comment"><span class="hljs-comment">//delete the old table and create a new table, or you can update the table accordingly</span></span>Tableutils.droptable (connectionsource, person.class,<span class="hljs-keyword"><span class="hljs-keyword">true</span></span>); OnCreate (database, connectionsource); }<span class="hljs-keyword"><span class="hljs-keyword">Catch</span></span>(SQLException E) {e.printstacktrace (); } }}</code></pre>Get DAO<p><p>For orm, DAO is a common practice for database Operations. Add the following code to the Helper:</p></p><pre class="prettyprint"><code class="language-java hljs "><span class="hljs-comment"><span class="hljs-comment">//declare a static instance</span></span><span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span>DBHelper instance;<span class="hljs-comment"><span class="hljs-comment">//statement DAO</span></span><span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>dao<person, integer> persondao;<span class="hljs-javadoc"><span class="hljs-javadoc">/** * Single case get the helper * *<span class="hljs-javadoctag"> @param</span> context *<span class="hljs-javadoctag"> @return</span> </span> */</span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span> <span class="hljs-keyword"><span class="hljs-keyword">synchronized</span></span>DBHelper<span class="hljs-title"><span class="hljs-title">Gethelper</span></span>(context Context) {context = Context.getapplicationcontext ();<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(instance = =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {<span class="hljs-keyword"><span class="hljs-keyword">synchronized</span></span>(databasehelper.class) {<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(instance = =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) Instance =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Databasehelper (context); } }<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>Instance }<span class="hljs-comment"><span class="hljs-comment">//get A dao, There are multiple tables, you may need to write multiple ways to get DAO</span></span><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span>dao<person, integer><span class="hljs-title"><span class="hljs-title">Getpersondao</span></span>()<span class="hljs-keyword"><span class="hljs-keyword">throws</span></span>SQLException {<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(persondao = =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {persondao = Getdao (person.class); }<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>persondao;}<span class="hljs-comment"><span class="hljs-comment">//release Resources</span></span><span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Close</span></span>() {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. Close (); Persondao =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>;}<span class="hljs-comment"><span class="hljs-comment">//there can also be a common method</span></span><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">synchronized</span></span>Dao<span class="hljs-title"><span class="hljs-title">Getdao</span></span>(Class Clazz)<span class="hljs-keyword"><span class="hljs-keyword">throws</span></span>SQLException {dao DAO =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>; String className = Clazz.getsimplename ();<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(daos.containskey (className)) {dao = Daos.get (className); }<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(dao = =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {dao =<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. Getdao (clazz); Daos.put (className, dao); }<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>Dao }</code></pre>To create a DAO class separately<pre class="prettyprint"><pre class="prettyprint"><code class="language-java hljs "><span class="hljs-keyword">public</span><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PersonDao</span> {</span> <span class="hljs-keyword">private</span> Context context; <span class="hljs-keyword">public</span><span class="hljs-title">PersonDao</span>(Context context) { <span class="hljs-keyword">this</span>.context = context; } <span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">add</span>(Person person) { <span class="hljs-keyword">try</span> { DBHelper.getHelper(context).getPersonDao().create(person); <span class="hljs-keyword">catch</span> (SQLException e) { e.printStackTrace(); } }}</code></pre></pre>Use<p><p>When You're ready, it's easy to Use.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class="language-java hljs "><span class="hljs-comment">// 获取helper</span>DBHelper helper = DBHelper.getHelper(getContext());<span class="hljs-comment">// 构造</span><span class="hljs-keyword">new</span> Person();xiaoming.setFirstName(<span class="hljs-string">"Ming"</span>);xiaoming.setLastName(<span class="hljs-string">"Xiao"</span>);<span class="hljs-comment">// 保存</span>helper.getPersonDao().create(xiaoming);</code></pre></pre> <p><p>Use ormlite--in Android Development</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