How to Use OrmLite persistence in Android

Source: Internet
Author: User

 

I believe everyone should know that there are many ways to save data in the Android system, but they can be summarized as four methods:

• Preferences

• Files

• Databases

• Network

Today, let's take a look at Databases. A lightweight database sqlite is built in the Android system. I believe there will be countless introductions on the Internet, so I will not talk nonsense here.

Because some friends are not familiar with SQL statements, or do not want to write them, or do not want to write them correctly, this article introduces OrmLite: Object Relational Mapping Lite (ORM Lite ).

OrmLite Provides Lightweight persistent Java objects to the SQL database, while also avoiding complexity and more standard ORM package overhead features. It supports the number of SQL databases that use JDBC, and also supports the native Android operating system database API to call sqlite. First we want to use it, go to its official http://ormlite.com to download the version you want, here I downloaded the latest 4.30. Next, let's write a simple project and give a detailed introduction to its usage.

 

After the project is created, we add the OrmLite jar package and create two entity classes. The example I used is the relationship between the customer and the order. One Customer corresponds to multiple orders.

 

 

Next, add attributes and fields to these two object classes. Note that there must be a constructor without parameters, which must be used when OrmLite creates an object.

Account: int aId, String aName; Order: int oid, String oName, Account account.

Then the ORMLite annotation is added. Annotation is a special code flag that has started in Java. to specify the classes and fields to store in the database, ORMLite supports its own annotations (@ DatabaseTable @ DatabaseField) or more standard annotations from javax. persistence package. Annotation is the easiest way to configure your class, but you can also use Java code or Spring XML configuration class. The following is a detailed description of the annotation:

The @ DatabaseTable annotation can have an optional TableName parameter to specify the name of the table corresponding to the class. If no table name is specified, the class name is used as the corresponding table name by default. For example:

@ DatabaseTable (tableName = "accounts ")

Public class Account {...} is like this. The table name corresponding to the Account class is accounts. If it is not specified, it is account.

 

The @ DatabaseField annotation can have the following fields:

Column name of columnName. Field name if not specified

A field of the dataType type. The general type is from the Java class field and does not need to be specified.

Default Value of defaultValue

Width: the default width is 0, indicating Unlimited

Whether canBeNull can be null. The default value is true.

The default id Primary Key is false.

The default value of the Self-increasing primary key of generatedId is false.

The serial number of the generatedIdSequence string name is similar to generatedId, but you can specify the sequence name. The default value is null.

Foreign key. The default value is false. The field cannot be of the original type. The class of the foreign key object must have an ID field (ID, generatedId, generatedIdSequence)

UseGetSet application get and set method access. The default value is false.

UnknownEnumName indicates that this field is a Java Enumeration type.

ThrowIfNull if it is null, the default value of false is thrown.

Whether persisted is stored in the database. The default value is true.

Format specifies the information format of a specific domain, such as the format of a date string.

Unique constraint of unique. The default value is false.

UniqueCombo is a unique row. All fields in the row become a unique constraint. For example, the fields firstName and lastName are "Zhang" and "Mei ", you cannot insert "Zhang" or "Mei" into the table, but you can insert "Zhang" or "quanmei ".

Whether to create an index. The default value is false.

The default value of uniqueIndex is false.

IndexName adds a name for the index in this field

UniqueIndexName adds a unique name for the index in this field

ForeignAutoRefresh whether to automatically refresh an Account foreign key object in the Order table when a foreign key object is queried. If an Order record is returned, whether to return the Account record is also returned. The default value is false.

MaxForeignAutoRefreshLevel this attribute is used to prevent infinite recursion or infinite loops and set the maximum level for automatic refresh.

AllowGeneratedIdInsert: whether to insert an ID field to overwrite the object of the generated ID. The default value is false.

ColumnDefinition defines columns. By default, the database type is used to automatically generate the required SQL statements to create columns. Therefore, this attribute is not commonly used.

When foreignAutoCreate inserts an object with a foreign key object, whether to automatically insert this foreign key object

Version row version protection when an object is updated to prevent data corruption to multiple objects at the same time

 

The above data source: http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite.html#SEC_Top

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.