Today we will introduce # afinal # To operate the android database sqlite.
# Afinal # is an android orm and ioc quick development framework, which includes four functions: Space id binding and event binding; display of network images (including a powerful cache framework), Database sqlite operations, and http data reading (supporting ajax READING );
This article mainly introduces the FinalDb component, one of the functions of afinal. For other components, please follow my blog and I will introduce them one by one in the future:
The FinalDb component of # afinal # is a lightweight orm framework of android. It is easy to use and can complete various database operations with one line of code.
First, create a test object class User. java.
01
Package com. devchina. ormdemo;
02
03
Import java. util. Date;
04
05
Public class User {
06
07
Private int id;
08
Private String name;
09
Private String email;
10
Private Date registerDate;
11
Private Double money;
12
13
/// // Getter and setter cannot be omitted ///////////////
14
Public int getId (){
15
Return id;
16
}
17
Public void setId (int id ){
18
This. id = id;
19
}
20
Public String getName (){
21
Return name;
22
}
23
Public void setName (String name ){
24
This. name = name;
25
}
26
Public String getEmail (){
27
Return email;
28
}
29
Public void setEmail (String email ){
30
This. email = email;
31
}
32
Public Date getRegisterDate (){
33
Return registerDate;
34
}
35
Public void setRegisterDate (Date registerDate ){
36
This. registerDate = registerDate;
37
}
38
Public Double getMoney (){
39
Return money;
40
}
41
Public void setMoney (Double money ){
42
This. money = money;
43
}
44
45
46
47
}
Note that getter and setter cannot be omitted in this object class, because finalDb of afinal will eventually call setter to assign values to attributes of the object class.
Now that the object class has been created, let's write our first demo:
AfinalOrmDemoActivity. java
01
Package com. devchina. ormdemo;
02
03
Import java. util. Date;
04
Import java. util. List;
05
06
Import net. tsz. afinal. FinalActivity;
07
Import net. tsz. afinal. FinalDb;
08
Import net. tsz. afinal. annotation. view. ViewInject;
09
Import android. OS. Bundle;
10
Import android. util. Log;
11
Import android. widget. TextView;
12
13
Public class AfinalOrmDemoActivity extends FinalActivity {
14
15
16
@ ViewInject (id = R. id. textView) TextView textView; // The afinal ioc function is used here.
17
18
@ Override
19
Public void onCreate (Bundle savedInstanceState ){
20
Super. onCreate (savedInstanceState );
21
SetContentView (R. layout. main );
22
23
FinalDb db = FinalDb. create (this );
24
25
User user = new User ();
26
User. setEmail ("afinal@tsz.net ");
27
User. setName ("Explorer ");
28
User. setRegisterDate (new Date ());
29
30
Db. save (user );
31
32
List <User> userList = db. findAll (User. class); // query all users
33
34
Log. e ("AfinalOrmDemoActivity", "user count:" + (userList! = Null? UserList. size (): 0 ));
35
36
TextView. setText (userList. get (0). getName () + ":" + user. getRegisterDate ());
37
}
38
}
It's easy, just a FinalDb db = FinalDb. create (this), and then db. save (user); then we can save the defined entity class to the database. Let's look at the output log:
What? In this way, it is saved to the sqlite database? But haven't we created databases or tables yet? How is it possible?
Here, I want to tell you how to create afinal on your own.
Let's take a look at the adt File Exploer to view the database directory. There is indeed a afinal. db File, such:
After exporting afinal. db, open afinal. db through the sqlite database, such:
At the same time, afinal automatically creates a table for us: com_devchina_ormdemo_User. From this we can see that afinal will automatically create a table with the Class Name: Let's look at the data in the following table:
Here, I believe you can understand the principle afinal automatically creates a database afinal for us. db also creates the table com_devchina_ormdemo_User for us. When saving the table, afinal automatically saves the data to the sqlite table.
At this time, it is estimated that the questions are raised again.
Afinal automatically creates the database afinal. db and the table com_devchina_ormdemo_User. However, we do not want to create a database afinal. db, or let us express com_devchina_ormdemo_User. What should we do?
Afinal has already thought about this.
Next, we will introduce the orm annotation function of afinal.
First, configure the Table name net. tsz. afinal. annotation. sqlite. Table in the database. Let's configure it for user and java:
01
Package com. devchina. ormdemo;
02
03
Import java. util. Date;
04
05
Import net. tsz. afinal. annotation. sqlite. Table;
06
07
@ Table (name = "user_test ")
08
Public class User {
09
10
Private int id;
11
Private String name;
12
Private String email;
13
Private Date registerDate;
14
Private Double money;
15
16
//// // Getter and setter ///////////////
17
// The code is too long. getter setter is omitted and cannot be omitted during development.
18
}
The only difference here is that there is an additional annotation @ Table (name = "user_test"). If we have configured this annotation, let's take a look at the database and Table created by afinal:
As a result, we can see that afinal automatically created the user_test table for us, but note that the afinal table com_devchina_ormdemo_User has not been deleted, so I would like to remind you that, when we re-design the structure or attributes of the class, manually delete the direct data first, otherwise there will be junk data stored in the database. Of course, you can also do it without deleting it, there will be no impact.
In the above description, a careful friend may notice a problem. afinal automatically regards the attribute of user id as the primary key. And automatically grow.
However, in our development process, our user may not have the id attribute. It may be userId or other attributes we like. What should we do?
It doesn't matter: afinal has prepared another annotation for us: net. tsz. afinal. annotation. sqlite. Id. Through this, we can define a primary key for our object class.
Afinal's primary key mechanism is:
When an annotation @ id is added to an attribute, the attribute is the primary key (only one primary key in a class), and the column name in the database is the name of the attribute, @ Id (column = "userId") when adding an annotation to the attribute, the column name stored in the database is userId. When this attribute is not available, afinal automatically goes back to the class lookup _ id attribute, if the _ id attribute does not exist, afinal will automatically search for the id attribute. If the id attribute does not exist, afinal returns an error. In the afinal orm rule, it indicates that the primary key must exist and only one primary key can exist. (currently, compound primary keys are not supported ).
Back to the question above, afinal automatically creates the database afinal. db. However, we do not want to create a database named afinal. db. What should we do?
Multiple methods are overloaded when afinal is created.
In the create method,
IsDebug indicates whether it is in debug mode. In debug mode, when afinal is used to operate the database, the log of the SQL statement is promised,
DbName is the name of the database.
So here, we can pass in the name of the database we want.
In fact, there are many other functions in the FinalDb module of afinal, such as one-to-many and many-to-one configuration and annotation. I am waiting for you to explore it.
The orm annotations of afinal include:
Id -------> Annotation
Property ------> Property Annotation
Table -------> data Table Annotation
ManyToOne --------> multiple-to-one Annotation
Onetow.---------> One-to-multiple annotation www.2cto.com
Transient -------> ignore attribute annotation (if this annotation is added to this attribute, afinal's orm function will ignore this attribute)