Activeandroid is an open-source database framework that makes it easier for us to use databases in Android, and today we'll look at the use of this database framework.
1. Introduction of Activeandroid
First create our own project, in our project introduced Activeandroid, introduced activeandroid need us to modify two places, a global gradle file, there is a local gradle file, modify the way as follows:
Global gradle file, add the following code below Jcenter ():
Mavencentral () maven {URL "https://oss.sonatype.org/content/repositories/snapshots/"}
Local Gradle File modification:
Compile ' com.michaelpardo:activeandroid:3.1.0-snapshot '
2. Create a table and database 2.1. Creation of tables
After introducing activeandroid into our project, we can start using this stuff, and if you use the Hibernate database framework in Java, you will find activeandroid very simple. Here we need to write a class that inherits from model this class, as follows:
@Table (name = "User_table", id = "_id") public class Userentity extends Model { @Column private String Username;
@Column private int age; @Column private String nickname; Public userentity () { } public userentity (Int., string nickname, String username) { this.age = age; This.nickname = nickname; This.username = Username; } public int getage () { return age; } public void Setage (int.) { this.age = age; } Public String Getnickname () { return nickname; } public void Setnickname (String nickname) { this.nickname = nickname; } Public String GetUserName () { return username; } public void Setusername (String username) { this.username = username; }}
Here are a few places to explain:
1. There are two values in the @table annotation on the class name, name= "user_table" and Id= "_id", where name= "user_table" means that the name of the generated table is user_table, the default is the name of the entity class, id= "_id" means the name of the table ID field, which is the default ID, but we typically define the ID field name in the table as _id in Android development, so Id= "_id" is set here
2. There is a @column annotation above each property name that indicates that the field is a field in the table and that the field is not a field in the table.
2.2. Modification of the database name and configuration of the version
The database name created by default is application.db, and if we need to modify the database name and do versioning we need to add the following node to the manifest file:
<meta-data android:name= "Aa_db_name" android:value= "pickrand.db"/> <meta-data android:name= "AA_DB_ VERSION "android:value=" 5 "/>
Where Aa_db_name represents the name of the database, Aa_db_version represents the database version number, do Android development cheese know that the version number is very important for a data, the version of the database upgrade is inseparable from the version number.
2.3 Database upgrades and table modifications
Database upgrade with Activeandroid a little bit of a hassle (personally), suppose I need to add a field to a table in my database. The following three steps are available:
1. First I need to modify the entity Class I defined above , assuming I want to add a gender field, then I need to modify the entity class as follows:
@Table (name = "User_table", id = "_id") public class Userentity extends Model { @Column private String username;
@Column private int age; @Column private String nickname; @Column//Gender field, the newly added field is private String gender;.....}
2. Modify the version number of the database in the manifest file, the new version number is the original version number plus 1, such as the original version number is 1, then I now change to 2
3. Add a migration script , and finally I need to add a database migration script. Add a subfolder in the Assets folder of my project called Migrations, add the migration script in this word folder, the name of the script is the latest version number of the current database. SQL, assuming my latest database version number is 2, my migration script will have a name of 2.sql. For my database upgrade above (add a field), the contents of my migration script are as follows:
ALTER TABLE user_table ADD COLUMN nickname;
OK, to complete the above three steps, my new field can be successfully added to my table.
3. Adding and deleting changes
After completing the above steps, we can then take a look at the database of additions and deletions to change the operation.
3.1 Increase
Add data too easy, I just need a new entity class to come out, and then call the entity class of the Save method can, gee, my entity class where the Save method? Don't forget, my entity class inherits from the model class OH. OK, let's look at a simple insert operation:
Userentity userentity = new Userentity (56, "John Doe", "Lisi"); Userentity.save ();
Create an instance of Userentity, and then call the instance's Save method. Then we take a look at the database:
OK, the data has been added to the table. Of course, this save method has a return value that represents the ID of the currently inserted data.
3.2 by deleting
For the delete operation, Activeandroid provides two solutions, one of which is a known ID and the user needs to delete the data by ID, as follows:
Userentity.delete (Userentity.class, 4);
Represents the deletion of data _id to 4.
The second method of deletion:
Delete delete = new Delete (); Delete.from (Userentity.class). WHERE ("Age= '"). and ("Nickname= ' John Doe '"). Execute ();
Represents the deletion of age=89 and nickname= John Doe data, and of course, a variety of left-connected right connections activeandroid are also supported.
3.3 Change
Update update = new update (userentity.class); Update.set ("Nickname= ' Harry '"). where ("Age= '"). Execute ();
The modification is also very simple, the above two lines of code means to change the age of 89 in the data of nickname to Harry. The and statement is not supported here, if cheese has multiple query conditions that can be executed together in a where statement, as follows:
Update update = new update (userentity.class); Update.set ("Nickname= ' Harry '"). where ("Age= '" and Nickname= ' Zhangsan ' "). Execute ();
3.4 Check
Select select = New Select (); list<userentity> list = Select.from (Userentity.class). Execute (); for (userentity userentity:list) { log.d ("GOOGLE_LENVE_FB", "Select:" + userentity.tostring ()); }
The above code means querying all the data in the User_table table, and the query results return a list collection directly. We can directly traverse the list set to display it. Query conditions also support where and a variety of left connections, right connection paging query, and so on. As follows:
list<userentity> list = Select.from (Userentity.class). The WHERE (""). and (""). As (""). GroupBy (""). Having (""). Offset (""). Limit (""). Execute ();
4. Advantages and Disadvantages
Overall, activeandroid is still very useful, greatly facilitates our database operations, simplifies the operation of the code, plus activeandroid version of the perfect support for the upgrade, making it more flexible.
But we also know that the convenience of this framework is at the expense of a part of the performance of the conditions obtained, the execution efficiency is certainly not as we write the crud statements, so cheese in the work according to their actual situation to choose whether to use the framework.
Above.
Play the Android database framework activeandroid use