Use Android Rapid development framework Afinal FINALDB to manipulate Android database

Source: Internet
Author: User

http://my.oschina.net/yangfuhai/blog/87459

Today to introduce you to the next #afinal# to operate the Android database SQLite.

#afinal # is an ORM, IOC rapid development framework that contains four functions: the ID binding and event binding of the space, the display of the network image (which contains a powerful caching framework), and the operation function of database SQLite. HTTP data reading function (AJAX-enabled reading);

#afinal # Open Source URL: https://github.com/yangfuhai/afinal

This article is mainly about afinal one of the functions of FINALDB components, other components please follow my blog, will be introduced in the future:

# afinal# 's FINALDB component is a lightweight ORM framework for Android, using a simple, one-line code to complete the various operational functions of the database.

Let's start by creating a test entity class User.java

?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 package com.devchina.ormdemo;import java.util.Date;public class User {        private int id;    private String name;    private String email;    private Date registerDate;    private Double money;        /////////////getter and setter 不能省略哦///////////////    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getEmail() {        return email;    }    public void setEmail(String email) {        this.email = email;    }    public Date getRegisterDate() {        return registerDate;    }    public void setRegisterDate(Date registerDate) {        this.registerDate = registerDate;    }    public Double getMoney() {        return money;    }    public void setMoney(Double money) {        this.money = money;    }            }

One thing to note about this entity class is that getter and setter cannot be omitted, because Afinal's finaldb will eventually call the setter to assign a value to the attribute of the entity class.

Now that the entity class has been created, let's write our first demo:

Afinalormdemoactivity.java

?
1234567891011121314151617181920212223242526272829303132333435363738 package com.devchina.ormdemo;import java.util.Date;import java.util.List;import net.tsz.afinal.FinalActivity;import net.tsz.afinal.FinalDb;import net.tsz.afinal.annotation.view.ViewInject;import android.os.Bundle;import android.util.Log;import android.widget.TextView;public class AfinalOrmDemoActivity extends FinalActivity {            @ViewInject(id=R.id.textView) TextView textView; //这里使用了afinal的ioc功能,以后将会讲到        @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                FinalDb db = FinalDb.create(this);                User user = new User();        user.setEmail("[email protected]");        user.setName("探索者");        user.setRegisterDate(new Date());                db.save(user);                 List<User> userList = db.findAll(User.class);//查询所有的用户                Log.e("AfinalOrmDemoActivity", "用户数量:"+ (userList!=null?userList.size():0));                textView.setText(userList.get(0).getName()+":"+user.getRegisterDate());    }}

Very simple, just a finaldb db = Finaldb.create (this), and then Db.save (user), you can save our defined entity class to the database. Let's look at the output log:

What the? So it's going to be saved to the SQLite database? But we haven't created a database or created a table yet? How can it be?

Here, I would like to tell you that Afinal himself to create, simple.

Let's look at the file exploer of ADT to see the database directory, and there is indeed a afinal.db file, such as:

After we have afinal.db out, we open afinal.db through the SQLite database, such as:

At the same time afinal automatically to us to create a table: Com_devchina_ormdemo_user, so that afinal will be the class name of the table name to automatically create the table: We'll look at the following table data:

Here, I believe you can understand, the principle of afinal automatically give us the creation of the database afinal.db at the same time we created a table Com_devchina_ormdemo_user, save the time, Afinal automatically save the data to the SQLite table.

At this time, I guess everyone's doubts are up again.

Afinal automatically creates the database Afinal.db and automatically creates the table Com_devchina_ormdemo_user. However, we do not want to create a database afinal.db, and do not want to let our expression Com_devchina_ormdemo_user, then we should do?

These are some, afinal have already thought well.

Next, let's introduce the ORM Annotation feature under Afinal.

First, the table name in the configuration database net.tsz.afinal.annotation.sqlite.Table, let's configure User,java:

?
123456789101112131415161718 package com.devchina.ormdemo; import java.util.Date;import net.tsz.afinal.annotation.sqlite.Table;@Table(name="user_test")public class User {        private int id;    private String name;    private String email;    private Date registerDate;    private Double money;        /////////////getter and setter///////////////    //代码太长,略getter setter,开发中不能省略}

The only difference here and above is that there is one more annotation @Table (name= "User_test"), as long as we configure this, and then we look at the database and tables Afinal created:

From this, we can see, afinal and automatically gave us a table user_test, but note that com_devchina_ormdemo_user this table afinal not to delete, so here also remind everyone, We re-design the structure or properties of the class, the first manually delete the direct data, otherwise there will be garbage data in the database, of course, do not delete can, there is no impact.

In the above narration, the attentive friend may notice a problem, Afinal automatically takes this attribute of user's ID as the primary key. and automatically grow.

However, in our development process, our user may not have the ID of this attribute ah, may be userid, or perhaps other properties we like, what to do?

No relationship: Afinal has prepared another note for us: net.tsz.afinal.annotation.sqlite.Id, by this we can define the primary key for our entity class.

The primary key mechanism for afinal is:

When adding annotation @Id to an attribute, the attribute is the primary key (there is only one primary key in a class), the name of the column that is saved in the database is the attribute, and @Id (column= "userId") when adding annotations to the attribute, the column name saved in the database is UserId, When this property does not, Afinal automatically go back to the class to find the _id property, _id property is not, Afinal will automatically look for the id attribute. If the id attribute is not even, then afinal error, afinal in the ORM rule, that must have a primary key, and only one (currently does not support composite primary key).

Back to the question, afinal to us to automatically create the database afinal.db, but, we do not want to create a database name is afinal.db, how to do?

Afinal is created with multiple overloads of methods.

In the method of Create,

Isdebug Indicates whether it is debug mode, in debug mode, when using Afinal to operate the database, it will promise the SQL statement log,

DBName is the name of the database.

So here, let's just pass in the name of the database we want.

In fact Afinal's FINALDB module, there are many other functions, such as a pair of many, many-to-one configuration and annotations and so on. Waiting for everyone to dig in.

Afinal's ORM annotations include:

Id-------> annotation Annotations

Property------> attribute Annotations

Table-------> Datasheet annotations

Manytoone--------> Many-to-one annotations

Onetomany---------> One-to-many annotations

Transient-------> Ignore attribute annotations (if the property adds this annotation, Afinal's ORM feature ignores the property)

SOURCE Generation:

http://download.csdn.net/detail/michael_yy/4740088

There is no jar bag, go to Afinal's GF website to download the latest jar package

Use Android Rapid development framework Afinal FINALDB to manipulate Android database

Related Article

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.