Create an android ORM framework opendroid (4) -- elegantly delete data, ormopendroid

Source: Internet
Author: User

Create an android ORM framework opendroid (4) -- elegantly delete data, ormopendroid

In the previous blog "Create an android ORM framework opendroid (iii) -- persistent data", we felt the process of saving data with opendroid. Today's blog shows how opendroid deletes data.

I still remember that we introduced the use of opendroid In the first blog "Create an android ORM framework opendroid (1)-use An 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);  

Another way to delete data is to use where.

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

Opendroid can be used to delete data in two ways, but these two methods can be used for most of the requirements.

The above section reviews the use of opendroid, but today's topic is to understand how opendroid deletes data from the database.

So... old rules, first locate

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

Start with the simplest!

/*** Delete data ** @ param klass class * @ param ids of the table to be deleted * @ return affects the number of rows */public static <T extends OpenDroid> int delete (Class <T> klass, int... ids) {if (ids. length = 0) {return delete (klass, null, null);} StringBuilder builder = new StringBuilder ("_ id in ("); string [] whereArgs = new String [ids. length]; buildIn (builder, whereArgs, ids); return delete (klass, builder. toString (), whereArgs );}

The method for deleting IDS is not long. Let's take a sentence to analyze it.

First 8 ~ Row 10 indicates that if no id is set, a static delete method is called. Let's look at this delete method later. Now let's proceed to the code.

In row 12, a StringBuilder is used to initialize an in statement. It is clear from here that using this delete method to delete data actually uses the in operation of the SQL statement.

Line 13: A new String Array. The length of the array is exactly the length of ids. Prepare for the in statement of the component below.

Line 14: we call the buildIn method to construct an in statement.

In row 15, an overloaded delete method is directly called to delete data. Here we mention that this delete method is the same as the delete method called in the above if statement.

Next let's take a look at the buildIn method.

/*** Assemble IN statement * @ param builder in statement * @ param whereArgs in content * @ param ids to assemble ids */private static 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 ("?) ");}

There is no difficulty in the entire buildIn method. The purpose here is to assemble a _ id in (?,?,?) In this form of statement, put ids in the String data we initialized above, so that we have prepared the where condition and Condition Parameters in SQL preprocessing?

Of course, there are two points that may be confusing:

1. Why is there no return value for this method? This method may not be very common, that is, to pass the return value in the form of a parameter. We all know that the java parameter is passed by reference, so as long as this parameter is not changed to the address, modifying the parameter content in this method will affect the caller.

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

Some friends may have noticed that the final ending point is outside the if. Is it wrong? There is no error here! See what the if condition is!

After the buildIn analysis is complete, we will return the delete method. The last sentence is to call another delete method. As for the parameter, you must have understood the value of the parameter.

Next let's take a look at this overloaded delete method.

/*** Delete data ** @ param klass class * @ param where condition * @ param whereArgs parameter of where condition * @ return 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, there are only two lines of code, and we are surprised to find that this method is also the second method we used to delete it. That's great, the two deletion methods will eventually be met in the same method!

How can this problem be solved? The Code in line 1st obtains the class name of klass, which indicates the deletion operation.

In the next line, we call the CRUD. delete method again, so let's follow up with the code and see CRUD. delete.

/*** Delete data ** @ param tableName table name * @ param where condition * @ param whereArgs parameter of the where condition * @ 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 ;}

There are two lines of code!

First, let's take a look at the parameters. The first parameter is the name of the table to be operated, the second parameter is the where condition, and the third parameter is the where condition parameter, the fourth parameter is, of course, the handle for operating the database.

In the Code body, the first sentence calls the android native delete operation to delete data based on conditions. The returned value is the number of deleted entries, and then returns the number, CRUD. the delete method has been executed!

The number of deleted entries will be returned to our business logic at most twice again.

Finally, let's review this process.

1. Our code calls the OpenDroid. delete method.

2. No matter which overload OpenDroid. delete method is called, The delete method that uses conditional deletion will be used.

3. delete an SQL in statement based on the input id and number of IDs.

4. Finally, the data is deleted in the delete method of CRUD, and the number of affected rows is returned.

The delete operation of opendroid has been completed, and it looks very high. Does the source code feel so simple?

So far, we have finished inserting and deleting opendroid data. In the following blog, we will continue to complete the opendroid data update and query code.

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: 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.