Build Android ORM Framework Opendroid (iv)--Elegant Delete data

Source: Internet
Author: User

In the previous blog, "Build Android ORM Framework Opendroid (iii)--persistent data", we feel the flow of opendroid to save the data, today's blog Let's take a look at opendroid how to delete data.

Remember when we introduced the use of opendroid in the first blog "Building Android ORM Framework Opendroid (a)--orm framework", let's review how to use opendroid to delete data.

int length = Opendroid.delete (Student.class, 1, 2, 3);    System.out.println (length);  

There is also a way to delete data by using the where bar.

int length = Opendroid.delete (Student.class, "_ID>?", "5");    System.out.println (length);

Opendroid is just two ways to delete data, but these two approaches are already suitable for most needs.

The above is a review of the use of opendroid, but our topic today is to understand how opendroid removes data from the database.

So... The usual, first to locate

Opendroid.delete (Student.class, 1, 2, 3);  

start with the simplest!

/**  * Delete Data  * @param klass the corresponding class  * @param IDs data for the table to be deleted  @return affect the number of rows */public  static <t extends opendroid> int Delete (class<t> klass, int ... ids) {      if (ids.length = = 0) {          return Delete (Klass, n ull, NULL);      }                StringBuilder builder = new StringBuilder ("_id in (");      string[] Whereargs = new String[ids.length];      Buildin (builder, Whereargs, IDS);                Return Delete (Klass, builder.tostring (), Whereargs);  

this method of deleting by ID is not long, let's analyze it one sentence at a time.

First 8~10 line, is a judge if there is no set ID, then call a static Delete method, this delete method we will wait to see, and now we go down the code.

12 rows, with a StringBuilder to initialize an in statement, from here we may already know that using this delete method to remove the data is actually using the SQL statement in operation.

13 lines, new A string array, the length of the array is exactly the length of IDs, for the following component in the statement to prepare.

14 Rows, we call the Buildin method to construct an in statement.

15 lines, directly called an overloaded Delete method to delete the data, here to mention, this delete method and our above if statement called the Delete method is the same.

Let's take a look at Buildin this way.

/**  * Assemble in statement  * @param builder in statement  * @param whereargs in content  * @param ids to assemble IDs */  private Stati c void Buildin (StringBuilder builder, string[] Whereargs,          int ... ids) {      if (Ids.length > 1) {for          (int i=0;i <ids.length-1;i++) {              Whereargs[i] = string.valueof (Ids[i]);              Builder.append ("?,");          }      }                Whereargs[ids.length-1] = string.valueof (ids[ids.length-1]);      Builder.append ("?)");  }

Throughout the entire Buildin method is not difficult, the role here is to assemble a _id in (?,?,?) The statement in this form, and put the IDs in the string data we initialized above, so that we are ready for SQL preprocessing where the conditions and conditions of the parameters?

There are, of course, two points that may be confusing:

1. Why does the method not return a value? This kind of writing may not be very common, that is, the return value is passed in the form of parameters, we all know that the parameter of Java is passed by reference, so long as the parameter is not changed to the address, the modification of the parameter content in the method will affect the caller.

2. Why is ids.length-1 in the for loop? Because what we need is (?,?,?) In this form, there is no "," after the last one, so we need to assemble the front length-1 first and then the final finishing work outside the loop.

There may be friends here who notice that the final finishing touches are outside the IF, is it wrong? There is nothing wrong with this! Please see if the condition is what!


After analyzing the Buildin, we return to the Delete method, the last sentence is to call another delete method, as for the parameters, we must have clear the value of the parameter.


Let's take a look at this overloaded Delete method.

/**  * Delete Data  * @param klass the class of the table to be deleted  @param the WHERE condition  * @param whereargs the WHERE Condition's parameter  * @retur n affects the number of rows  *  /public static <t extends opendroid> int delete (class<t> klass, string where,          string ... Whereargs) {      String tableName = Klass.getsimplename ();      Return Crud.delete (TableName, where, Whereargs, ssqlitedatabase);  }

haha, only two lines of code, and we are surprised to find that this method is also our second way to delete, too good, two ways to delete the end will be in the same method to meet!

So simple, how to do it? The 1th line of code gets the class name of the Klass, which is, of course, the indication of our delete operation.

In the next line we call the Crud.delete method again, then we follow the code, go to see Crud.delete bar.

/** *  Delete data  * @param tableName table name  * @param the WHERE Condition  * @param whereargs the WHERE condition parameter  * @param db    Database  * @return affects the number of rows *  /protected static <t extends opendroid> int Delete (string tableName, string where ,          string[] Whereargs, Sqlitedatabase db) {      int count = Db.delete (TableName, where, Whereargs);      return count;  }

another two lines of code!

First, take a look at the parameters, the first parameter is the name of the table to be manipulated, the second parameter is the Where condition, the third parameter is the where condition parameter, and the fourth parameter is of course the handle of the database we manipulate.

In the code body, the first sentence is called the Android native delete operation to delete the data according to the conditions, the return value is the number of deleted entries, and then return the number, Crud.delete method execution is complete!

The number of deleted entries is returned up to two times again, eventually returning to our business logic.


Finally, let's review the process.

1. Our code calls the Opendroid.delete method

2, regardless of which overloaded Opendroid.delete method is called, the last one will come to the Delete method that uses the condition deletion

3, using the ID to delete, is simply based on the number of IDs and IDs to the component of a SQL in statement

4. Finally, the deletion of data is done in the crud Delete method, and the number of rows affected is returned.


Opendroid Delete operation we have been smooth over, looks very high-end, a look at the source is not feeling so simple?

So far, we have finished opendroid insert data and delete data, and in the next blog, we will continue to complete the Opendroid update data and query data code.

Build Android ORM Framework Opendroid (iv)--Elegant Delete data

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.